As pointed out by redtree, I learned that Pelican now uses github style code block notation by default.
So please ignore the content of this article! Pelican is the best!
I use Pelican as a blogging tool. By the way, when I was a kid, when I went out to the garden, I suddenly saw a pelican in front of me. In front of me, who was dismayed, the pelican screamed "gar" and flew away.
Even if I told people about it, I was disappointed that no one could believe it. As time goes by and my memory fades, "Is that really a pelican? Isn't it a mistake on a pelican flight?" I sometimes think that.
Well, Pelican blogs can be written using Markdown notation, Especially for those who write a lot of source code, than plain Markdown Github-Flavored-Markdown (GFM) is better I think there are many people who are used to it and find it easy to write.
Unfortunately, you usually can't use GFM.
Python has a library called misaka that can use GFM notation, so let's make it available.
I'm sorry for those who read the meaninglessly long "Introduction", It's a crazy, pretty defeated fix. I think that it should be realized by plug-ins etc., but it is troublesome.
By the way, I'm trying the Python version with 2.7 recommended by Pelican. The OS is Mac OS 10.9.
Install with pip.
$ pip install misaka
Markdown parsing is done in a file called readers.py
, so
I will mess with that file directly.
If you don't know where the files are, use pip show
to find them.
(pelican)comme@garcons$ pip show pelican
---
Name: pelican
Version: 3.3
Location: /Users/comme/.virtualenvs/pelican/lib/python2.7/site-packages
Requires: feedgenerator, jinja2, pygments, docutils, pytz, blinker, unidecode, six
You can find readers.py
in the pelican
directory of the directory indicated by location.
Corrected here on the 19th line
try:
from markdown import Markdown
except ImportError:
Markdown = False # NOQA
↓ Correct like this
try:
from markdown import Markdown
import misaka
import pygments
except ImportError:
Markdown = False # NOQA
Fixed here around line 215
def read(self, source_path):
"""Parse content and metadata of markdown files"""
self._md = Markdown(extensions=self.extensions)
with pelican_open(source_path) as text:
content = self._md.convert(text)
metadata = self._parse_metadata(self._md.Meta)
return content, metadata
↓ Correct like this
def read(self, source_path):
"""Parse content and metadata of markdown files"""
self._md = Markdown(extensions=self.extensions)
with pelican_open(source_path) as text:
content = self._md.convert(text)
metadata = self._parse_metadata(self._md.Meta)
renderer = self.BleepRenderer()
misaka_md = misaka.Markdown(renderer,
extensions=misaka.EXT_FENCED_CODE | misaka.EXT_NO_INTRA_EMPHASIS)
with pelican_open(source_path) as text:
misaka_content = misaka_md.render(text)
return misaka_content, metadata
class BleepRenderer(misaka.HtmlRenderer, misaka.SmartyPants):
def block_code(self, text, lang):
if not lang:
return '\n<pre><code>%s</code></pre>\n' % escape(text.strip())
lexer = pygments.lexers.get_lexer_by_name(lang, stripall=True)
formatter = pygments.formatters.HtmlFormatter()
return pygments.highlight(text, lexer, formatter)
That's it.
Now, like kobito and github,
```python
def func():
print('uuuuuuuuuuwaaaaaaaaaaaaaaaaaaahhhhhhhh')
```
If you write like, and make html
, you can get the html you expected.
--- a/readers.py
+++ b/readers.py
@@ -18,6 +18,8 @@ except ImportError:
docutils = False
try:
from markdown import Markdown
+ import misaka
+ import pygments
except ImportError:
Markdown = False # NOQA
try:
@@ -215,7 +217,22 @@ class MarkdownReader(BaseReader):
content = self._md.convert(text)
metadata = self._parse_metadata(self._md.Meta)
- return content, metadata
+
+ renderer = self.BleepRenderer()
+ misaka_md = misaka.Markdown(renderer,
+ extensions=misaka.EXT_FENCED_CODE | misaka.EXT_NO_INTRA_EMPHASIS)
+ with pelican_open(source_path) as text:
+ misaka_content = misaka_md.render(text)
+
+ return misaka_content, metadata
+
+ class BleepRenderer(misaka.HtmlRenderer, misaka.SmartyPants):
+ def block_code(self, text, lang):
+ if not lang:
+ return '\n<pre><code>%s</code></pre>\n' % escape(text.strip())
+ lexer = pygments.lexers.get_lexer_by_name(lang, stripall=True)
+ formatter = pygments.formatters.HtmlFormatter()
+ return pygments.highlight(text, lexer, formatter)
class HTMLReader(BaseReader):
Recommended Posts