Merge pull request #197 from Jenselme/creole_reader

Creole reader: add source code highlighting support
This commit is contained in:
Justin Mayer
2014-05-02 08:02:54 -07:00
2 changed files with 56 additions and 5 deletions

View File

@@ -1,7 +1,7 @@
# Creole Reader # Creole Reader
This plugins allows you to write your posts using the wikicreole syntax. Give to This plugins allows you to write your posts using the wikicreole syntax. Give to
these files the creole extension. The medata are between `<<header>> <</header>>` these files the creole extension. The metadata are between `<<header>> <</header>>`
tags. tags.
## Dependency ## Dependency
@@ -9,7 +9,9 @@ This plugin relies on [python-creole](https://pypi.python.org/pypi/python-creole
`pip install python-creole` `pip install python-creole`
## Syntax ## Syntax
Use ** for strong, // for emphasis, one = for 1st level titles. Use ** for strong, // for emphasis, one = for 1st level titles. Please use the
following macro for code highlighting:
`<<code ext=".file_extension">> <</code>>`
For the complete syntax, look at: http://www.wikicreole.org/ For the complete syntax, look at: http://www.wikicreole.org/
@@ -24,12 +26,16 @@ date: 2013-12-12
= Title 1 = Title 1
== Title 2 == Title 2
Some nice texte with **strong** and //emphasis//. Some nice text with **strong** and //emphasis//.
* A nice list * A nice list
** With subelements ** With sub-elements
* Python * Python
<<code ext=".py">>
print("Hello World")
<</code>>
# An ordered list # An ordered list
# A second item # A second item
``` ```

View File

@@ -21,6 +21,14 @@ try:
except ImportError: except ImportError:
creole = False creole = False
try:
from pygments import lexers
from pygments.formatters import HtmlFormatter
from pygments import highlight
PYGMENTS = True
except:
PYGMENTS = False
class CreoleReader(readers.BaseReader): class CreoleReader(readers.BaseReader):
enabled = creole enabled = creole
@@ -39,6 +47,42 @@ class CreoleReader(readers.BaseReader):
self._metadata[name] = self.process_metadata(name, value) self._metadata[name] = self.process_metadata(name, value)
return u'' return u''
def _no_highlight(self, text):
html = u'\n<pre><code>{}</code></pre>\n'.format(text)
return html
def _get_lexer(self, source_type, code):
try:
return lexers.get_lexer_by_name(source_type)
except:
return lexers.guess_lexer(code)
def _get_formatter(self):
formatter = HtmlFormatter(lineos = True, encoding='utf-8',
style='colorful', outencoding='utf-8',
cssclass='pygments')
return formatter
def _parse_code_macro(self, ext, text):
if not PYGMENTS:
return self._no_highlight(text)
try:
source_type = ''
if '.' in ext:
source_type = ext.strip().split('.')[1]
else:
source_type = ext.strip()
except IndexError:
source_type = ''
lexer = self._get_lexer(source_type, text)
formatter = self._get_formatter()
try:
return highlight(text, lexer, formatter).decode('utf-8')
except:
return self._no_highlight(text)
# You need to have a read method, which takes a filename and returns # You need to have a read method, which takes a filename and returns
# some content and the associated metadata. # some content and the associated metadata.
def read(self, source_path): def read(self, source_path):
@@ -46,7 +90,8 @@ class CreoleReader(readers.BaseReader):
self._metadata = {} self._metadata = {}
with pelican_open(source_path) as text: with pelican_open(source_path) as text:
content = creole2html(text, macros={'header': self._parse_header_macro}) content = creole2html(text, macros={'header': self._parse_header_macro,
'code': self._parse_code_macro})
return content, self._metadata return content, self._metadata
def add_reader(readers): def add_reader(readers):