4

I'm using Pelican along with pygments to generate syntax highlighting for my MD documents, however, i'm having trouble applying the highlighting to indented blocks.

For example:

  1. Text 1

  2. Text 2

      Code Here
    
  3. Text 3

The 10 space method works great for placing code as a sub-element of a list, however, i can't apply syntax highlighting as i normally would outside of a list like

```python

Can someone explain how i can have the code as a sub-element of the list WITH syntax highlighting?

Update: I've figured it out now. For anyone that's confused in the future here's how my code looks.

1. MD List El 1
2. MD List El 2
3. MD List El 3
(blank line)
(2 tabs):::python
        code here
(blank line)
4. MD List El 3

Doing it this way successfully generated the list numbers for me.

asaji
  • 398
  • 4
  • 12

2 Answers2

9

Pelican's documentation states:

For Markdown, include the language identifier just above the code block, indenting both the identifier and code:

A block of text.

    :::identifier
    <code goes here>

The specified identifier (e.g. python, ruby) should be one that appears on the list of available lexers.

Of course, an extra level of indent is needed to nest that code block in a list. Markdown's indentation level is 4 spaces, so 4 spaces to nest inside a list plus 4 spaces to make it a code block = 8 spaces of indent.

Therefore this should do the trick (with spaces represented by "·" for illustrative purposes):

1. Text 1

2. Text 2

········:::python
········#Code Here

3. Text 3

You could also use tabs (represented by "→" for illustrative purposes):

1. Text 1

2. Text 2

→→:::python
→→#Code Here

3. Text 3

If you are still not getting syntax highlighting, are you sure you installed all of the dependencies? Code highlighting is done by Pygments which needs to be installed in addition to Python-Markdown.

You also need to make sure that you have created/copied Pygments compatible css files to your project and linked to them from your html templates. I'm not sure if Pelican gives you this by default or not, but without it, the highlighting won't be visible even if it is there.

Waylan
  • 37,164
  • 12
  • 83
  • 109
  • thanks! the :::python tag did the trick. I had tried the 2 tab or 8 space before with the ``` operator and the ~~~ operator and for one reason or the other, those won't generate the list the way it should be generated, however, the ::: operator does. – asaji Jan 24 '14 at 06:39
  • @asaji, I'm confused about why this answer worked and mine did not, as this answer simply restates my first suggestion, and you commented saying that it didn't work. In any case, glad you got it working. – ChrisGPT was on strike Jan 24 '14 at 13:10
  • @Chris Unfortunately it seemed to be the combination of the ::: operator AND the 8 spaces that i needed to use to get it to work, nevertheless your answer was very important for getting to the conclusion :) – asaji Jan 24 '14 at 16:25
  • @asaji The fenced code blocks (using ~~~ or \`\`\`) only work at the root level of the document. In other words, you can't use them nested in a list. Hopefully I will have the time to rectify that soon. – Waylan Feb 20 '14 at 01:44
  • @Chris your answer didn't work because you only provided part of the answer. It is pretty obvious from the question that nesting as much of an issue as language identification. Actually, it was more of an issue as Pygments will try to guess the language if it is not defined. So if asaji had gotten the nesting correct he might have had syntax highlighting even without a language identifier. Without the proper nesting his code blocks were not being parsed as code blocks, so they never got sent to Pygments at all. – Waylan Feb 20 '14 at 01:52
1

Pelican seems to use this library for its Markdown support. According to its documentation you should be able to do something like

:::python
import re
# ...

or

#!python
import re
# ...

In either case, you should get a rendered

import re
# ...

Without that first line.

If those don't work, you can try this HTML comment syntax, which is supported by a number of Markdown parsers, including the one used for Stack Overflow:

* Item 1
* Item 2

<!-- language: lang-python -->

    import re
    # ...
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • Hi Chris, i actually tried the :::python step before. First time, i put 0 spaces before it and added it to the line above the code, second time i added 10 spaces before it. Both times, it didn't recognize it as python. I went ahead and tried your #!python and still nada. – asaji Jan 23 '14 at 18:43
  • @asaji, and the `#!python` didn't work either? I have updated my answer with another suggestion. – ChrisGPT was on strike Jan 23 '14 at 18:54
  • unfortunately still no luck. Heres an image i made to describe the conundrum. http://imgur.com/TtSHrZp In the first set of images, i don't "indent" and therefore the code doesn't appear as a subelement of the list, however, syntax highlighting works. In the second set of images, i do indent by 10 spaces and it works, however, it won't pick up the language – asaji Jan 23 '14 at 19:33
  • @asaji, outdent the comment to the left margin. It should not be part of the code block. – ChrisGPT was on strike Jan 23 '14 at 20:22
  • no luck with that either.. also tried a lot of the stuff here: http://stackoverflow.com/questions/6235995/markdown-github-syntax-highlighting-of-code-block-as-a-child-of-a-list and still no luck. – asaji Jan 24 '14 at 00:17