Syntax highlighted code block in markdown indented list paragraph - python

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):

Related

vscode python avoid indent of multiline string

So here is an example of a multiline string in vscode/python:
Cursor is after the p , and then you press enter, and end up like this:
i.e. the string ends up indented, which seems what you almost never want - why have an arbitratly amount of whitespace on the next line of this string ?
Is there any way change this in vscode, i.e. for multiline strings, it should end up with this:
I think this problem is related to different coding styles of different people.
For example,
def example(x):
if x:
a = '''
This is help
'''
def example(x):
if x:
a = '''This is help
'''
The automatic indenting of vscode line breaks is based on code blocks. If you want Vscode can identify multiline string, I think it would be better to submit future request in github. I've submitted this issue for you.
I am not 100% sure if what OP meant is just to refer to the indentation in the editor (namely, VSC) or if, by this:
i.e. the string ends up indented, which seems what you almost never want - why have an arbitrary amount of white space on the next line of this string?
...they also meant to refer to the actual output of the multi-line string,
(or also, just in case anybody else finds this post looking for a way to avoid this affecting the actual output of the multi-line string), I'd like to add as a complementary answer (cannot comment yet) that this was already beautifully answered here.
If that's the case and you're reading this for that reason, in short, all you want is to import the standard lib 'inspect' and post-process your string with it, using the cleandoc method.
Without breaking the indentation in your IDE, this method makes sure to give you the string output you actually expected:
All leading whitespace is removed from the first line. Any leading whitespace that can be uniformly removed from the second line onwards is removed. Empty lines at the beginning and end are subsequently removed. Also, all tabs are expanded to spaces.
(From the docs link above)
Hope that helps anyone.

Python: What Does This Syntax Error Mean? [duplicate]

I think this is perfectly valid.
if False:
print(1)
print(2)
However, it gives me an invalid syntax error in Python REPL.
Why is it?
On Python 3.6.5 (x64), Windows 10 RS4
As pointed out by user2357112, this behaviour is explained in https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming,
The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount.
The REPL can only read and evaluate one statement at a time.
You entered two statements at once.
This is possible because the REPL cannot decide if the third line is going to continue the if construction or start a whole new statement. It has to assume the former to allow indented blocks at all.
You have to make it clear to the REPL that your previous statement is finished before starting a new one.
Wrong version seems to be the most possible since, in the error it shows print. In the older python versions, print was used as print"ok", I see your operating system is windows so you can just directly download python3 from https://python.org/ have a nice day!

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.

Syntax highlighting markdown lists

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

Cannot post Python code to the website rosalind.info

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.

Categories

Resources