Discussion:
Weird Stuff (Markdown, syntax highlighting and Python)
(too old to reply)
Gilmeh Serda
2024-05-26 06:28:51 UTC
Permalink
The web claims (I think on all pages I've read about Markdown and Python)
that this code should work, with some very minor variants on the topic:

```python

import os

with open(os.path.join('/home/user/apath', 'somefile')) as f:
print(f.read())
```

However, that is not the case. At least not for me (using Python 3.12.3).
If instead I type it:

#!python

import os

with open(os.path.join('/home/user/apath', 'somefile')) as f:
print(f.read())

As an indented block (four spaces) and a shebang, THEN it works. You even
get line numbers by default.

N.b. if you don't know, you also need to generate a css file using
pygments to make this work.

Not until I started to read the markdown source code and its docs pages,
the coin dropped.

I'm posting this for other Markdown newbies that otherwise probably would
spend hours trying to make it work.


Speaking of Markdown. Does anybody out there have any idea how to turn on
table borders, adjust them (color/width/etc.) and such things? Currently I
have to add HTML to do so, which works, but isn't very nice. I'd hate to
spend an additional day or two, hunting for this info.

References:
https://pypi.org/project/Markdown/
https://python-markdown.github.io/
--
Gilmeh

"Reality is that which, when you stop believing in it, doesn't go away".
-- Philip K. Dick
Lawrence D'Oliveiro
2024-05-26 07:42:14 UTC
Permalink
Post by Gilmeh Serda
The web claims (I think on all pages I've read about Markdown and
Python) that this code should work, with some very minor variants on the
```python
import os
print(f.read())
```
I just tried it in a Jupyter notebook. Changed the cell type to Markdown,
pasted the above as its contents, hit shift-enter, and it looked just like
syntax-coloured Python code.
Gilmeh Serda
2024-05-26 08:59:44 UTC
Permalink
Post by Lawrence D'Oliveiro
I just tried it in a Jupyter notebook. Changed the cell type to
Markdown, pasted the above as its contents, hit shift-enter, and it
looked just like syntax-coloured Python code.
I've been trying some more and apparently you also have to indent the
backticks and all of the code, which unfortunately makes the backticks
visible, which, well, is ugly. Perhaps there is a setting for it
somewhere, not sure.

It smells as if JN has code that corrects the behavior, whereas plain
Python hasn't.

I think I prefer the other method without the backticks. It's cleaner and
more to the point of what it does.


Cheers,
--
Gilmeh

Rule #1: The Boss is always right. Rule #2: If the Boss is wrong, see Rule
#1.
Lawrence D'Oliveiro
2024-05-26 21:01:12 UTC
Permalink
Post by Gilmeh Serda
Post by Lawrence D'Oliveiro
I just tried it in a Jupyter notebook. Changed the cell type to
Markdown, pasted the above as its contents, hit shift-enter, and it
looked just like syntax-coloured Python code.
I've been trying some more and apparently you also have to indent the
backticks and all of the code, which unfortunately makes the backticks
visible ...
I didn’t need to do that.
Grant Edwards
2024-05-27 18:41:15 UTC
Permalink
Post by Gilmeh Serda
The web claims (I think on all pages I've read about Markdown and Python)
```python
import os
print(f.read())
```
However, that is not the case.
For me, that block formats as expected using Python markdown.

What do you mean by "this code should work [...] that is not the case"?

What markdown rendering engine are you using?

--
Grant
Gilmeh Serda
2024-05-27 22:47:22 UTC
Permalink
Post by Grant Edwards
What markdown rendering engine are you using?
$ python
Python 3.12.3 (main, Apr 23 2024, 09:16:07) [GCC 13.2.1 20240417] on linux
Type "help", "copyright", "credits" or "license" for more information.
Post by Grant Edwards
import markdown, pygments
markdown.__version__
'3.6'
Post by Grant Edwards
pygments.__version__
'2.17.2'

I've been thinking maybe it's some weird text encoding issue. I can use
the indented-by-four-spaces-and-a-shebang for now, it's no big deal.
--
Gilmeh

Satellite Safety Tip #14: If you see a bright streak in the sky coming at
you, duck.
dn
2024-05-27 20:58:17 UTC
Permalink
With reference to another reply here, the "Weird stuff" came from
reading the question, finding it unclear, and only later realising that
whereas most people write Markdown-formatted documents for later
processing, or perhaps docstrings in Markdown-format for collection by
documentation systems; here, the objective appears to be using Python to
generate Markdown.

How much have you used Markdown to any serious degree, before attempting
this feat?
Post by Gilmeh Serda
The web claims (I think on all pages I've read about Markdown and Python)
There are so many "variants", the problem is not "minor"!

Markdown users learn to use their tool (again, see @Grant's question)
and work within the implementation of that "variant".

Like any other non-standardised tool, the users of some particular
'version' often fail to realise that others using different versions may
not enjoy the same experience. Plus-one for standardisation!


At the end of the message, the web.refs reveal use of a package which is
based upon a variant of Markdown that is 20-years old(!), albeit with
some updates to suit yet another variant. Neither variant-author famous
for collaboration. The phrase YMMV springs to mind...


Some ten years ago, an effort was made to standardise Markup, and it
ended-up being called CommonMark. Why is it not called "Standard
Markdown" one might ask? Because the fellow who 'invented' Markdown
objected. This very objection has likely led directly to your
confusions, because the particular PyPi package is based upon that
original definition...

Whereas, Markdown 3.6 is the most-recently updated Markdown search-hit
on PyPi today, have you tried any of the others (which, ironically, may
offer more recent and/or more standardised coverage)?


This has worked in all of the Markdown processors I have used or tried-out:

The (?reasonable) 'common-core', offers single back-ticks for code,
triple back-ticks for a code-block, and the latter with or without a
language specification which *usually* kicks-in syntax highlighting.
Post by Gilmeh Serda
```python
import os
print(f.read())
```
However, that is not the case. At least not for me (using Python 3.12.3).
It's not Python 3 that is the problem. It is the "Markdown 3.6" package!
I've not seen the hash-bang combination in-the-wild (but YMMV!)
Post by Gilmeh Serda
#!python
import os
print(f.read())
As an indented block (four spaces) and a shebang, THEN it works. You even
get line numbers by default.
An indented-block is NOT necessarily the same as a code-block - just as
"code" is not necessarily "Python".

Line numbers are great - although if a code snippet is extracted from
the middle of some example code-file, the original line-numbers won't
line-up with Markdown's...
Post by Gilmeh Serda
N.b. if you don't know, you also need to generate a css file using
pygments to make this work.
That's not what the package's docs suggest:
https://python-markdown.github.io/extensions/fenced_code_blocks/
Post by Gilmeh Serda
Not until I started to read the markdown source code and its docs pages,
the coin dropped.
I'm posting this for other Markdown newbies that otherwise probably would
spend hours trying to make it work.
Speaking of Markdown. Does anybody out there have any idea how to turn on
table borders, adjust them (color/width/etc.) and such things? Currently I
have to add HTML to do so, which works, but isn't very nice. I'd hate to
spend an additional day or two, hunting for this info.
Again, heavily dependent upon the tool in-use. For example, most SSGs
and doc-tools (which accept Markdown) have a .css or theming-system
which enables 'decorations'.
Post by Gilmeh Serda
https://pypi.org/project/Markdown/
https://python-markdown.github.io/
Further reading:
https://en.wikipedia.org/wiki/Markdown
https://commonmark.org
https://pypi.org/search/?q=markdown
--
Regards,
=dn
Thomas Passin
2024-05-27 18:08:55 UTC
Permalink
Post by Gilmeh Serda
The web claims (I think on all pages I've read about Markdown and Python)
```python
import os
print(f.read())
```
There are different flavors of Markdown, so that might be a factor so
far as details of the block are concerned.

What do you mean by it not "working"? What do you see and what did you
expect to see? What did you see different when you used the next example?

How did you generate the output HTML file?
Post by Gilmeh Serda
However, that is not the case. At least not for me (using Python 3.12.3).
#!python
import os
print(f.read())
As an indented block (four spaces) and a shebang, THEN it works. You even
get line numbers by default.
N.b. if you don't know, you also need to generate a css file using
pygments to make this work.
Not until I started to read the markdown source code and its docs pages,
the coin dropped.
I'm posting this for other Markdown newbies that otherwise probably would
spend hours trying to make it work.
Speaking of Markdown. Does anybody out there have any idea how to turn on
table borders, adjust them (color/width/etc.) and such things? Currently I
have to add HTML to do so, which works, but isn't very nice. I'd hate to
spend an additional day or two, hunting for this info.
https://pypi.org/project/Markdown/
https://python-markdown.github.io/
Gilmeh Serda
2024-05-28 18:49:06 UTC
Permalink
Solved by using a different method.
--
Gilmeh

Hedonist for hire... no job too easy!
o1bigtenor
2024-05-29 10:44:50 UTC
Permalink
On Tue, May 28, 2024 at 9:48 PM Gilmeh Serda via Python-list <
Post by Gilmeh Serda
Solved by using a different method.
- - - And that was how?

TIA
dn
2024-05-29 21:59:24 UTC
Permalink
Post by Gilmeh Serda
Solved by using a different method.
Hedonist for hire... no job too easy!
This combination of sig-file and content seems sadly ironic.


How about CONTRIBUTING to the community by explaining 'the solution' to
people who may find a similar problem - in the similar manner to the
various members who have helped YOU, voluntarily (and despite the
paucity of source-information and response)?
--
Regards,
=dn
Loading...