summaryrefslogtreecommitdiff
path: root/external/markdown-processor.py
diff options
context:
space:
mode:
authorChristian Hammond <christian@beanbaginc.com>2016-11-04 16:57:38 -0700
committerChristian Hammond <christian@beanbaginc.com>2016-11-04 16:57:38 -0700
commit6ded9db39463372e5205a36bea72d6de516ece69 (patch)
tree1d1f497cc99dd44d2ee7e2c3daa35965157ff924 /external/markdown-processor.py
downloadpygments-git-6ded9db39463372e5205a36bea72d6de516ece69.tar.gz
Add support for partials and path segments for Handlebars.
This introduces support for some missing features to the Handlebars lexer: Partials and path segments. Partials mostly appeared to work before, but the `>` in `{{> ... }}` would appear as a syntax error, as could other components of the partial. This change introduces support for: * Standard partials: `{{> partialName}}` * Partials with parameters: `{{> partialName varname="value"}}` * Ddynamic partials: `{{> (partialFunc)}}` * Ddynamic partials with lookups: `{{> (lookup ../path "partialName")}}` * Partial blocks: `{{> @partial-block}}` * Inline partials: `{{#*inline}}..{{/inline}}` It also introduces support for path segments, which can reference content in the current context or in a parent context. For instance, `this.name`, `this/name`, `./name`, `../name`, `this/name`, etc. These are all now tracked as variables.
Diffstat (limited to 'external/markdown-processor.py')
-rw-r--r--external/markdown-processor.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/external/markdown-processor.py b/external/markdown-processor.py
new file mode 100644
index 00000000..a3e178ec
--- /dev/null
+++ b/external/markdown-processor.py
@@ -0,0 +1,67 @@
+# -*- coding: utf-8 -*-
+"""
+ The Pygments Markdown Preprocessor
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+ This fragment is a Markdown_ preprocessor that renders source code
+ to HTML via Pygments. To use it, invoke Markdown like so::
+
+ import markdown
+
+ html = markdown.markdown(someText, extensions=[CodeBlockExtension()])
+
+ This uses CSS classes by default, so use
+ ``pygmentize -S <some style> -f html > pygments.css``
+ to create a stylesheet to be added to the website.
+
+ You can then highlight source code in your markdown markup::
+
+ [sourcecode:lexer]
+ some code
+ [/sourcecode]
+
+ .. _Markdown: https://pypi.python.org/pypi/Markdown
+
+ :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+"""
+
+# Options
+# ~~~~~~~
+
+# Set to True if you want inline CSS styles instead of classes
+INLINESTYLES = False
+
+
+import re
+
+from markdown.preprocessors import Preprocessor
+from markdown.extensions import Extension
+
+from pygments import highlight
+from pygments.formatters import HtmlFormatter
+from pygments.lexers import get_lexer_by_name, TextLexer
+
+
+class CodeBlockPreprocessor(Preprocessor):
+
+ pattern = re.compile(r'\[sourcecode:(.+?)\](.+?)\[/sourcecode\]', re.S)
+
+ formatter = HtmlFormatter(noclasses=INLINESTYLES)
+
+ def run(self, lines):
+ def repl(m):
+ try:
+ lexer = get_lexer_by_name(m.group(1))
+ except ValueError:
+ lexer = TextLexer()
+ code = highlight(m.group(2), lexer, self.formatter)
+ code = code.replace('\n\n', '\n&nbsp;\n').replace('\n', '<br />')
+ return '\n\n<div class="code">%s</div>\n\n' % code
+ joined_lines = "\n".join(lines)
+ joined_lines = self.pattern.sub(repl, joined_lines)
+ return joined_lines.split("\n")
+
+class CodeBlockExtension(Extension):
+ def extendMarkdown(self, md, md_globals):
+ md.preprocessors.add('CodeBlockPreprocessor', CodeBlockPreprocessor(), '_begin')