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.
Related
I've trouble finding a library to convert simple HTML (with <b>, <i>, <p>, <li> ...) to a simple representation. Obviously this can't match HTML spec very far, but I don't need fancy things. For instance lynx is good for the task (except bold and italic are ignored and could probably be translated in some ANSI attributes):
$ echo "<b>hello</b> <p>this is a <i>list</i> <ul><li>foo</li><li>bar</li></ul></p>" |
lynx -stdin -dump
hello
this is a list
* foo
* bar
The ideal solution would be a python library. Otherwise I will stick to use lynx... So any command better than the one I've proposed here would also be accepted.
There is html2text which is not quite what wanted, but could match some other viewers constraints.
It produces text from html. This text is following Markdown format. So there are no use of ANSI attributes for instance. However, as Markdown is meant to be a visual text-only format, it can satisfy probably some needs.
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
# ...
I am trying to post a sample solution, written in Python, to rosalind.info.
I tried following their instructions:
To highlight code, add a shebang styled first line :::lexername to your code. Replace lexername with the lexer keyword for the language that you want to be highlighted as shown in the list of Pygments lexers."
However, I can't get it to work.
I have tried setting the first line to:
:::python
:::PythonLexer
#!:::python
#!:::PythonLexer
but it just appears as ordinary text.
It seems your first attempt was correct, but you did not click the 'Submit' button to view your code with the lexer applied.
In order to see the code with syntax highlighting, you must first submit your response. The WYSIWYG editor provided below the markdown box does not perform syntax highlighting. In order to see your code with proper highlighting you would type something like the following into the box.
:::python
print "Hello World"
which will look something like
print "Hello World"
once you click the 'Submit' button and view your response. You will have the option to edit your submission if you want to change things later.
Joshua's answer has linked you to the place where you can determine which lexer name you want to use. Simply choose type the corresponding 'short name' for the highlighting you would like to apply.
You could try
#!:::python3
Source: http://pygments.org/docs/lexers/
Two ways that worked for me were:
Indent the whole code block with 4 spaces. Make the first indented line either
#! python or
:::python.
The first version also adds line numbers to the formatted code.
Instead, you can also surround the code block with lines containing only triple backticks ```, in which case you don't need to indent the code with spaces. As in the previous case, make the first line after the opening backtick line either
#! python or
:::python.
The first version adds line numbers, as mentioned above.
As mentioned before by others, you need to "Submit" before you see the fully formatted result.
I'm writing an application, part of whose functionality is to generate LaTeX CVs, so I find myself in a situation where I have strings like
\begin{document}
\title{Papers by AUTHOR}
\author{}
\date{}
\maketitle
\begin{enumerate}
%% LIST OF PAPERS
%% Please comment out anything between here and the
%% first \item
%% Please send any updates or corrections to the list to
%% XXXEMAIL???XXX
%\usepackage[pdftex, ...
which I would like to populate with dynamic information, e.g. an email address. Due to the format of LaTeX itself, .format with the {email} syntax won't work, and neither will using a dictionary with the %(email)s syntax. Edit: in particular, strings like "\begin{document}" (a command in LaTeX) should be left literally as they are, without replacement from .format, and strings like "%%" (a comment in LaTeX) should also be left, without replacement from a populating dictionary. What's a reasonable way to do this?
Why won't this work?
>>> output = r'\author{{email}}'.format(email='user#example.org')
>>> print output
\author{email}
edit: Use double curly braces to "escape" literal curly braces that only LaTeX understands:
>>> output = r'\begin{{document}} ... \author{{{email}}}'.format(
... email='user#example.org')
>>> print output
\begin{document} ... \author{user#example.org}
You may not use the new format syntax to avoid escaping the { and }.
That should work:
>>> a = r'''
\title{%(title)s}
\author{%(author)s}
\begin{document}'''
>>> b = a % {'title': 'My Title', 'author': 'Me, Of course'}
>>> print(b)
\title{My Title}
\author{Me, Of course}
\begin{document}
You should use raw strings r'something' to avoid escaping \ as \\.
PS: You should take a look on txt2tags, a Python script to convert t2t formatted text into html, latex, markdown etc. Check the source code to see how these conversions are done.
I have a blog written in django that I am attempting to add syntax highlighting to. The posts are written and stored in the database as textile markup. Here is how they are supposed to be rendered via template engine:
{{ body|textile|pygmentize|safe }}
It renders all the HTML correctly and the code gets highlighted, but some characters within the code blocks are being escaped. Specifically double quotes, single quotes, and greater than signs.
Here is the Pygments filter I am using: http://djangosnippets.org/snippets/416/
I'm not sure which filter is actually putting the escaped characters in there or how to make it stop that. Any suggestions?
shameless plug to me answering this on another page:
https://stackoverflow.com/a/10138569/1224926
the problem is beautifulsoup (rightly) assumes code is unsafe. but if you parse it into a tree, and pass that in, it works. So your line:
code.replaceWith(highlight(code.string, lexer, HtmlFormatter()))
should become:
code.replaceWith(BeautifulSoup(highlight(code.string, lexer, HtmlFormatter())))
and you get what you would expect.