How to use Pyigs in Pelican with Markdown?

TL; DR: I'm trying to do CSS line numbering in a pelican, writing in markdowns. Pigments are used indirectly, and you cannot pass parameters to it, so I cannot separate the lines and there is no CSS selector for the "new line".

Using Markdown in Pelican, I can generate code blocks using the CodeHilite extension. The Pelican does not support the use of pigments directly if you use Markdown ... only RST (and ... not for converting everything to RST).

So what I tried:

MD_EXTENSIONS = [
'codehilite(css_class=highlight,linenums=False,guess_lang=True,use_pygments=True)',
'extra']

and

:::python
<div class="line">import __main__ as main</div>

and

PYGMENTS_RST_OPTIONS = {'classprefix': 'pgcss', 'linenos': 'table'}

Can I display line numbers? Yes. Can I get them to continue the next block of code? No. And that's why I want to use CSS line numbering ... its way is easier to control when the numbering starts and stops.

, .

+4
2

, , - CodeHilite ( ). ( ), , , PYTHONPATH (, "sitepackages", , Python). , , - Python.

, . Pelican - Python, ( : yourmodule.py = > yourmodule) .

from yourmodule import CodeHiliteExtension
MD_EXTENSIONS = [
CodeHiliteExtension(css_class='highlight', linenums=False),
'extra']

, CodeHiliteExtension , , .

. ( ), setup.py, . tutorial , Markdown.

, , , . , Pygments 117. - .


, reStructuredText, , , . Docutils . , Pygments, reStructeredText. , CSS - . , Pygments .

CodeHilite, Pyigs , , . , , , , . , .


, () :

diff --git a/markdown/extensions/codehilite.py b/markdown/extensions/codehilite.py
index 0657c37..fbd127d 100644
--- a/markdown/extensions/codehilite.py
+++ b/markdown/extensions/codehilite.py
@@ -115,12 +115,18 @@ class CodeHilite(object):
                 except ValueError:
                     lexer = get_lexer_by_name('text')
             formatter = get_formatter_by_name('html',
-                                              linenos=self.linenums,
+                                              linenos=self.linenums if self.linenumes != 'css' else False,
                                               cssclass=self.css_class,
                                               style=self.style,
                                               noclasses=self.noclasses,
                                               hl_lines=self.hl_lines)
-            return highlight(self.src, lexer, formatter)
+            result = highlight(self.src, lexer, formatter)
+            if self.linenums == 'css':
+                lines = result.split('\n')
+                for i, line in enumerate(lines):
+                    lines[i] = '<div class="line">%s</div>' % line
+                result = '\n'.join(lines)
+            return result
         else:
             # just escape and build markup usable by JS highlighting libs
             txt = self.src.replace('&', '&amp;')

, , Pyigs JavaScript . , JavaScript .

+6

TL; DR

pelicanconf.py, :

# for highlighting code-segments
# PYGMENTS_RST_OPTIONS = {'cssclass': 'codehilite', 'linenos': 'table'}   # disable RST options
MD_EXTENSIONS = ['codehilite(noclasses=True, pygments_style=native)', 'extra']  # enable MD options

,

pip install pygments markdown
+1
source

All Articles