snippets.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. """
  2. Snippet ---8<---.
  3. pymdownx.snippet
  4. Inject snippets
  5. MIT license.
  6. Copyright (c) 2017 Isaac Muse <isaacmuse@gmail.com>
  7. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  8. documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  9. the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all copies or substantial portions
  12. of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14. TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  16. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. DEALINGS IN THE SOFTWARE.
  18. """
  19. from markdown import Extension
  20. from markdown.preprocessors import Preprocessor
  21. import re
  22. import codecs
  23. import os
  24. class SnippetPreprocessor(Preprocessor):
  25. """Handle snippets in Markdown content."""
  26. RE_ALL_SNIPPETS = re.compile(
  27. r'''(?x)
  28. ^(?P<space>[ \t]*)
  29. (?P<all>
  30. (?P<inline_marker>-{2,}8<-{2,}[ \t]+)
  31. (?P<snippet>(?:"(?:\\"|[^"\n\r])+?"|'(?:\\'|[^'\n\r])+?'))(?![ \t]) |
  32. (?P<block_marker>-{2,}8<-{2,})(?![ \t])
  33. )\r?$
  34. '''
  35. )
  36. RE_SNIPPET = re.compile(
  37. r'''(?x)
  38. ^(?P<space>[ \t]*)
  39. (?P<snippet>.*?)\r?$
  40. '''
  41. )
  42. def __init__(self, config, md):
  43. """Initialize."""
  44. self.base_path = config.get('base_path')
  45. self.encoding = config.get('encoding')
  46. self.check_paths = config.get('check_paths')
  47. self.tab_length = md.tab_length
  48. super(SnippetPreprocessor, self).__init__()
  49. def parse_snippets(self, lines, file_name=None):
  50. """Parse snippets snippet."""
  51. new_lines = []
  52. inline = False
  53. block = False
  54. for line in lines:
  55. inline = False
  56. m = self.RE_ALL_SNIPPETS.match(line)
  57. if m:
  58. if block and m.group('inline_marker'):
  59. # Don't use inline notation directly under a block.
  60. # It's okay if inline is used again in sub file though.
  61. continue
  62. elif m.group('inline_marker'):
  63. # Inline
  64. inline = True
  65. else:
  66. # Block
  67. block = not block
  68. continue
  69. elif not block:
  70. # Not in snippet, and we didn't find an inline,
  71. # so just a normal line
  72. new_lines.append(line)
  73. continue
  74. if block and not inline:
  75. # We are in a block and we didn't just find a nested inline
  76. # So check if a block path
  77. m = self.RE_SNIPPET.match(line)
  78. if m:
  79. # Get spaces and snippet path. Remove quotes if inline.
  80. space = m.group('space').expandtabs(self.tab_length)
  81. path = m.group('snippet')[1:-1].strip() if inline else m.group('snippet').strip()
  82. if not inline:
  83. # Block path handling
  84. if not path:
  85. # Empty path line, insert a blank line
  86. new_lines.append('')
  87. continue
  88. if path.startswith('; '):
  89. # path stats with '#', consider it commented out.
  90. # We just removing the line.
  91. continue
  92. snippet = os.path.join(self.base_path, path)
  93. if snippet:
  94. if os.path.exists(snippet):
  95. if snippet in self.seen:
  96. # This is in the stack and we don't want an infinite loop!
  97. continue
  98. if file_name:
  99. # Track this file.
  100. self.seen.add(file_name)
  101. try:
  102. with codecs.open(snippet, 'r', encoding=self.encoding) as f:
  103. new_lines.extend(
  104. [space + l2 for l2 in self.parse_snippets([l.rstrip('\r\n') for l in f], snippet)]
  105. )
  106. except Exception: # pragma: no cover
  107. pass
  108. if file_name:
  109. self.seen.remove(file_name)
  110. elif self.check_paths:
  111. raise IOError("Snippet at path %s could not be found" % path)
  112. return new_lines
  113. def run(self, lines):
  114. """Process snippets."""
  115. self.seen = set()
  116. return self.parse_snippets(lines)
  117. class SnippetExtension(Extension):
  118. """Snippet extension."""
  119. def __init__(self, *args, **kwargs):
  120. """Initialize."""
  121. self.config = {
  122. 'base_path': [".", "Base path for snippet paths - Default: \"\""],
  123. 'encoding': ["utf-8", "Encoding of snippets - Default: \"utf-8\""],
  124. 'check_paths': [False, "Make the build fail if a snippet can't be found - Default: \"false\""]
  125. }
  126. super(SnippetExtension, self).__init__(*args, **kwargs)
  127. def extendMarkdown(self, md):
  128. """Register the extension."""
  129. self.md = md
  130. md.registerExtension(self)
  131. config = self.getConfigs()
  132. snippet = SnippetPreprocessor(config, md)
  133. md.preprocessors.register(snippet, "snippet", 32)
  134. def makeExtension(*args, **kwargs):
  135. """Return extension."""
  136. return SnippetExtension(*args, **kwargs)