Syntax highlighting markdown lists - python

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:
Text 1
Text 2
Code Here
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.

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.

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
# ...

Related

Syntax highlighted code block in markdown indented list paragraph

Markdown lets me indent a paragraph twice under a list item to make it part of that list.
1. List item
Paragraph will be rendered inside of list item.
2. Other list item
Other paragraph.
This would be code.
3. But what about syntax highlighted code?
```bash
echo "This won't work"
```
I can also indent twice as much to make code blocks.
BUT how do I use syntax highlighted code blocks such as these inside a list paragraph? Is it at all possible?
In my situation I'm using mkdocs, a python static HTML generator using markdown.
Background on why I'm asking is because such lists actually make for a really nice design when you're writing step by step guides.
What about this:
3. But what about syntax highlighted code?
~~~~ bash
echo "This won't work"
~~~~
It renders like this in Firefox (using Stackedit):

How to automatically insert spaces to make empty lines in Python files indent?

I recently encountered the common "unexpected indent" problem when trying to evaluate python code by copying them from PyDev and Emacs into a python interpreter.
After trying to fix tab/spaces and some searches, I found the cause in this answer:
This error can also occur when pasting something into the Python
interpreter (terminal/console).
Note that the interpreter interprets an empty line as the end of an
expression, so if you paste in something like
def my_function():
x = 3
y = 7
the interpreter will interpret the empty line before y = 7 as the end
of the expression ...
, which is exactly the case in my situation. And there is also a comment to the answer which points out a solution:
key being that blank lines within the function definition are fine,
but they still must have the initial whitespace since Python
interprets any blank line as the end of the function
But the solution is impractical as I have many empty lines that are problematic for the interpreter. My question is:
Is there a method/tool to automatically insert the right number of initial whitespaces to empty lines so that I can copy-and-paste my code from an editor to an interpreter?
Don't bother with inserting spaces. Tell the interpreter to execute a block of text instead:
>>> exec(r'''
<paste your code>
''')
The r''' ... ''' tripple-quoted string preserves escapes and newlines. Sometimes (though in my experience, rarely) you need to use r""" ... """ instead, when the code block contains tripple-quoted strings using single quotes.
Another option is to switch to using IPython to do your day-to-day testing of pasted code, which handles pasted code with blank lines natively.

Markdown: Is there a way to specify raw text in markdown?

I am using python to generated markdown, is there a way to specify a "raw" string in markdown terms?
i.e.
<-- magic markdown formatting to indicate not to format the following text
# This is a comment
--- end of text ---
<-- end of magic markdown formatting
Should appear as is without letting markdown touch it at all.
If you want to add comments, you could use the syntax for code.
In github flavoured markdown it normally uses ``` (3 backticks)
In the python-markdown it is like Stack overflow, where you put 4 spaces in front of the line.
if you do not want to format it like code, you can simply escape the markdown syntax like :
\# comment
Will display # comment rather than the word "comment" as a heading.

How can I get the minted package to render code blocks with copyable indentation?

To the naked eye, minted (Konrad Rudolph's LaTeX package for code highlighting using the Pygments library) faithfully renders code blocks that are passed to it, displaying them with whatever indentation was contained in the source code.
If, however, you attempt to copy and paste code from one of those blocks, you'll notice that their visible indentation is achieved using non-copyable
spaces, such that the pasted code loses each line's leading spaces. This is
particularly problematic with Python code blocks, because in Python
indentation has actual meaning as a part of the code.
So, here's my question: Is there some way to get minted to render code blocks that, when
copy-and-pasted, keep the indentation of the source code they display?
For examples of what I mean, see any of the several indented code blocks in
the minted manual (found
here), or compile
the following minimal-ish reproducible example:
\documentclass{article}
\usepackage{minted}
\newminted[python]{python}{frame=single}
\begin{document}
\begin{python}
def example1():
if verbose:
print 'Running example1'
verbose = True
example1()
\end{python}
\end{document}
This works in Acrobat Reader, at least on my system, but not in SumatraPDF and perhaps some other programs. There may be other, better solutions.
\usepackage{color}
\usepackage{minted}
\newminted[python]{python}{frame=single}
\fvset{showspaces}
\renewcommand\FancyVerbSpace{\textcolor{white}{\char32}}
This sets fancyvrb, which Pygments uses for formatting output, to use visible space characters (␣), and then makes the characters "invisible" by making them white. Ultimately, this does end up being a TeX question, since Pygments is using the fancyvrb package for its output, and the trick is to get fancyvrb to create (leading) spaces that can be copied.

IronPython: Is there an alternative to significant whitespace?

For rapidly changing business rules, I'm storing IronPython fragments in XML files. So far this has been working out well, but I'm starting to get to the point where I need more that just one-line expressions.
The problem is that XML and significant whilespace don't play well together. Before I abandon it for another language, I would like to know if IronPython has an alternative syntax.
IronPython doesn't have an alternate syntax. It's an implementation of Python, and Python uses significant indentation (all languages use significant whitespace, not sure why we talk about whitespace when it's only indentation that's unusual in the Python case).
>>> from __future__ import braces
File "<stdin>", line 1
from __future__ import braces
^
SyntaxError: not a chance
All I want is something that will let my users write code like
Ummm... Don't do this. You don't actually want this. In the long run, this will cause endless little issues because you're trying to force too much content into an attribute.
Do this.
<Rule Name="Markup">
<Formula>(Account.PricingLevel + 1) * .05</Formula>
</Rule>
You should try not to have significant, meaningful stuff in attributes. As a general XML design policy, you should use tags and save attributes for names and ID's and the like. When you look at well-done XSD's and DTD's, you see that attributes are used minimally.
Having the body of the rule in a separate tag (not an attribute) saves much pain. And it allows a tool to provide correct CDATA sections. Use a tool like Altova's XML Spy to assure that your tags have space preserved properly.
I think you can set the xml:space="preserve" attribute or use a <![CDATA[ to avoid other issues, with for example quotes and greater equal signs.
Apart from the already mentioned CDATA sections, there's pindent.py which can, among others, fix broken indentation based on comments a la #end if - to quote the linked file:
When called as "pindent -r" it assumes its input is a Python program with block-closing comments but with its indentation messed up, and outputs a properly indented version.
...
A "block-closing comment" is a comment of the form '# end <keyword>' where is the keyword that opened the block. If the opening keyword is 'def' or 'class', the function or class name may be repeated in the block-closing comment as well. Here is an example of a program fully augmented with block-closing comments:
def foobar(a, b):
if a == b:
a = a+1
elif a < b:
b = b-1
if b > a: a = a-1
# end if
else:
print 'oops!'
# end if
# end def foobar
It's bundeled with CPython, but if IronPython doesn't have it, just grab it from the repository.

Categories

Resources