nim-lang / RFCs

A repository for your Nim proposals.
136 stars 23 forks source link

universal inline code block syntax for nim doc+rst #355

Open timotheecour opened 3 years ago

timotheecour commented 3 years ago

/cc @a-mr

proposal

add a syntax for doc comments and rst files allowing writing arbitrary inline code blocks with optional syntax indicator, as follows:

hello1 ``` abc ``` rest of comment # simplest cases, matches {abc}
hello2 ```py abc ``` rest of comment # matches {abc} syntax colored with py language (ditto below)
hello3 ```py abc  ``` rest of comment # matches {abc }
hello4 ```py  abc  ``` rest of comment # matches { abc }
hello5 ```py   a bc ``` rest of comment # matches {  a bc}
hello6 ```py `abc` ``` rest of comment # matches {`abc`}
hello7 ````py ```abc ```` rest of comment # matches {```abc} (just increase backticks as needed)

the syntax is:

prefix = N backticks (N>=3)
1 optional alphanumeric identifier indicating language
1 space
S: arbitrary string that cannot contain `prefix` but can contain spaces and backticks
1 space
prefix

benefits

note

this proposal is simple (both to understand and to implement), allows encoding anything, and has 0 edge cases that I know of.

open question

narimiran commented 3 years ago

How important/crucial is this? Shouldn't we focus on more important stuff than spending our time on implementing this and then fixing future bugs?

Thumb down from me, sorry.

Araq commented 3 years ago

I like the idea but ideally we should copy from some existing, well-known markdown or rst extension. If there are any...

timotheecour commented 3 years ago

maybe such extensions exist but I'm not awareof it, and people often complain about markdown/rst lacking such a feature. I don't like any of the workarounds mentioned in those links, which is why I ended up suggesting the simple solution in this RFC.

markdown:

rst:

the preservation of whitespace cannot be guaranteed. If the preservation of line breaks and/or other whitespace is important, literal blocks should be used

The end-string must be immediately preceded by non-whitespace.". So it looks like it's not possible per definition. This answer has introduced a custom role for accomplishing a similar task however it requires modifying the Sphinx code

a-mr commented 3 years ago

There is already too much confusing syntax for adding even more.

Trailing/leading whitespace use cases are too marginal, let us leave it be.

For inline syntax highlighting we'd better focus on implementation of existing RST syntax:

See `def f(): return 3`:python:
 or `def f(): return 3`:py:

(In rst2html.py it already works if such roles are defined:

.. role:: python(code)
   :language: python
.. role:: py(code)
   :language: python

)

timotheecour commented 3 years ago

@a-mr the existing syntax is the one that's confusing and limited, with lots of edge cases it cannot handle, and people complain about it (see links in https://github.com/nim-lang/RFCs/issues/355#issuecomment-806488730).

In rst2html.py it already works [...] Trailing/leading whitespace use cases are too marginal, let us leave it be.

are these too marginal too?

Instead of copying bad design, this RFC replaces it with something that's:

ie, it replaces something that sucks by something that doesn't suck.

eg, say I want inline block to represent these syntax-highlighted snippets for bash:

text before ```bash export MYPWD=`pwd`; ``` text after

or even this: ```bash export MYPWD=`pwd` ```

or this: ```cmd cd\``` (eg for windows cmd prompt to cd to root)

with rst2html how do you do that? say even the 1st case:

.. role:: bash(code)
   :language: bash

can't work: backtick needs escaping:
`export MYPWD=`pwd`;`:bash:

can't work with double backticks: roles are for single backticks:
``export MYPWD=`pwd`;``:bash:

no syntax highlighting:
``export MYPWD=`pwd```

this doesn't render correctly in rst2html:
`export MYPWD=\`pwd\`;`:bash:

this doesn't render correctly in rst2html:
`export MYPWD=\`pwd\`;\\`:bash:

gives:

image

with this RFC, this is simply:

text before ```bash export MYPWD=`pwd`; ``` text after
text before ```bash export MYPWD=`pwd` ``` text after
text before ```cmd cd\ ``` text after
a-mr commented 3 years ago

@timotheecour ,

Yes,

those are very minor use cases.

For them we can just advice users to use full .. code-block:: nim or its normal Markdown counterpart.

a-mr commented 3 years ago

A problem with trailing space looks completely artificial.

If you really have it then currently just explain it by a few English words :-)