Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replace newlines with <br/> to make markdown stop freaking out on bla…

…nk lines.
  • Loading branch information...
commit ce60b1d0d127add1cd827abbd751ffdc23a9b8a5 1 parent 8a66daf
@EnigmaCurry authored
Showing with 26 additions and 24 deletions.
  1. +26 −24 blogofile/site_init/blog_filters/_filters/syntax_highlight.py
View
50 blogofile/site_init/blog_filters/_filters/syntax_highlight.py
@@ -4,25 +4,9 @@
import pygments
from pygments import formatters, util, lexers
import blogofile_bf as bf
-
-config = {"name": "Syntax Highlighter",
- "description": "Highlights blocks of code based on syntax",
- "author": "Ryan McGuire",
- "css_dir": "/css",
- "preload_styles": []}
-
-
-def init():
- #This filter normally only loads pygments styles when needed.
- #This will force a particular style to get loaded at startup.
- for style in bf.config.filters.syntax_highlight.preload_styles:
- css_class = "pygments_{0}".format(style)
- formatter = pygments.formatters.HtmlFormatter(
- linenos=False, cssclass=css_class, style=style)
- write_pygments_css(style, formatter)
-
-example = """
+#Example usage:
+"""
This is normal text.
@@ -68,6 +52,23 @@ def to_s
This is normal text
"""
+config = {"name": "Syntax Highlighter",
+ "description": "Highlights blocks of code based on syntax",
+ "author": "Ryan McGuire",
+ "css_dir": "/css",
+ "preload_styles": []}
+
+
+def init():
+ #This filter normally only loads pygments styles when needed.
+ #This will force a particular style to get loaded at startup.
+ for style in bf.config.filters.syntax_highlight.preload_styles:
+ css_class = "pygments_{0}".format(style)
+ formatter = pygments.formatters.HtmlFormatter(
+ linenos=False, cssclass=css_class, style=style)
+ write_pygments_css(style, formatter)
+
+
css_files_written = set()
code_block_re = re.compile(
@@ -90,19 +91,20 @@ def to_s
"[,\r\n]" # ends in a comma or newline
)
-
def highlight_code(code, language, formatter):
try:
lexer = pygments.lexers.get_lexer_by_name(language)
except pygments.util.ClassNotFound:
lexer = pygments.lexers.get_lexer_by_name("text")
- #Highlight with pygments and surround by blank lines
- #(blank lines required for markdown syntax)
- highlighted = "\n\n{0}\n\n".format(
- pygments.highlight(code, lexer, formatter))
+ #Highlight with pygments
+ highlighted = pygments.highlight(code, lexer, formatter)
+ #Replace newlines with breaks - markdown freaks out otherwise.
+ highlighted = highlighted.replace("\n","<br/>")
+ #Surround the text with newlines so markdown etc parse properly:
+ highlighted = "\n\n{0}\n\n".format(highlighted)
+ print highlighted
return highlighted
-
def parse_args(args):
#Make sure the args are newline terminated (req'd by regex)
opts = {}
Please sign in to comment.
Something went wrong with that request. Please try again.