meta.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # markdown is released under the BSD license
  2. # Copyright 2007, 2008 The Python Markdown Project (v. 1.7 and later)
  3. # Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
  4. # Copyright 2004 Manfred Stienstra (the original version)
  5. #
  6. # All rights reserved.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions are met:
  10. #
  11. # * Redistributions of source code must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. # * Redistributions in binary form must reproduce the above copyright
  14. # notice, this list of conditions and the following disclaimer in the
  15. # documentation and/or other materials provided with the distribution.
  16. # * Neither the name of the <organization> nor the
  17. # names of its contributors may be used to endorse or promote products
  18. # derived from this software without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE PYTHON MARKDOWN PROJECT ''AS IS'' AND ANY
  21. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. # DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTORS TO THE PYTHON MARKDOWN PROJECT
  24. # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. # POSSIBILITY OF SUCH DAMAGE.
  31. """
  32. Meta Data Extension for Python-Markdown
  33. =======================================
  34. This extension adds Meta Data handling to markdown.
  35. Basic Usage:
  36. >>> import markdown
  37. >>> text = '''Title: A Test Doc.
  38. ... Author: Waylan Limberg
  39. ... John Doe
  40. ... Blank_Data:
  41. ...
  42. ... The body. This is paragraph one.
  43. ... '''
  44. >>> md = markdown.Markdown(['meta'])
  45. >>> print md.convert(text)
  46. <p>The body. This is paragraph one.</p>
  47. >>> print md.Meta
  48. {u'blank_data': [u''], u'author': [u'Waylan Limberg', u'John Doe'], u'title': [u'A Test Doc.']}
  49. Make sure text without Meta Data still works (markdown < 1.6b returns a <p>).
  50. >>> text = ' Some Code - not extra lines of meta data.'
  51. >>> md = markdown.Markdown(['meta'])
  52. >>> print md.convert(text)
  53. <pre><code>Some Code - not extra lines of meta data.
  54. </code></pre>
  55. >>> md.Meta
  56. {}
  57. Copyright 2007-2008 [Waylan Limberg](http://achinghead.com).
  58. Project website: <http://packages.python.org/Markdown/meta_data.html>
  59. Contact: markdown@freewisdom.org
  60. License: BSD (see ../LICENSE.md for details)
  61. """
  62. from __future__ import absolute_import
  63. from __future__ import unicode_literals
  64. from . import Extension
  65. from ..preprocessors import Preprocessor
  66. import re
  67. # Global Vars
  68. META_RE = re.compile(r'^[ ]{0,3}(?P<key>[A-Za-z0-9_-]+):\s*(?P<value>.*)')
  69. META_MORE_RE = re.compile(r'^[ ]{4,}(?P<value>.*)')
  70. class MetaExtension (Extension):
  71. """ Meta-Data extension for Python-Markdown. """
  72. def extendMarkdown(self, md, md_globals):
  73. """ Add MetaPreprocessor to Markdown instance. """
  74. md.preprocessors.add("meta", MetaPreprocessor(md), "_begin")
  75. class MetaPreprocessor(Preprocessor):
  76. """ Get Meta-Data. """
  77. def run(self, lines):
  78. """ Parse Meta-Data and store in Markdown.Meta. """
  79. meta = {}
  80. key = None
  81. while 1:
  82. line = lines.pop(0)
  83. if line.strip() == '':
  84. break # blank line - done
  85. m1 = META_RE.match(line)
  86. if m1:
  87. key = m1.group('key').lower().strip()
  88. value = m1.group('value').strip()
  89. try:
  90. meta[key].append(value)
  91. except KeyError:
  92. meta[key] = [value]
  93. else:
  94. m2 = META_MORE_RE.match(line)
  95. if m2 and key:
  96. # Add another line to existing key
  97. meta[key].append(m2.group('value').strip())
  98. else:
  99. lines.insert(0, line)
  100. break # no meta data - done
  101. self.markdown.Meta = meta
  102. return lines
  103. def makeExtension(configs={}):
  104. return MetaExtension(configs=configs)