Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
timotheecour opened this issue Mar 24, 2021 · 7 comments
Open

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

timotheecour opened this issue Mar 24, 2021 · 7 comments

Comments

@timotheecour
Copy link
Member

timotheecour commented Mar 24, 2021

/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

  • this can encode anything, including beginning/trailing whitespace, beginning/trailing backticks
  • this can have optional language indicating how to syntax highlight
  • this is non-ambiguous and easy to understand, no complex escaping is needed

note

  • markdown cannot handle beginning/trailing whitespace, and, depending on the markdown interpreter, consecutive inner spaces inside will be collapsed to 1 whitespace
  • rst also cannot handle beginning/trailing whitespace or arbitrary sequences of backticks, neither with interpreted literals (single backtick) nor with inline literals (double backtick), and the escaping rules are messy and complex to implement (nim's implementation is still broken, and there are multiple standards anyways)
  • backward compatibility: triple backticks are broken (see inline triple backtick foo.bar in doc comment incorrectly identifies foo as lang, stripping it in docs Nim#17314) so this isn't an issue; instead we're giving new meaning to something that's currently broken
  • this would not be the go-to inline code syntax (which would remain single backticks) but would be the sytnax to use for cases not well handled by single backticks.

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

open question

  • I think we can also make this proposal work with N>=2 or even N>=1 instead of N>=3 given that the proposed syntax is currently illegal in those cases, so there would be no collusion with existing semantics, thanks to trailing space before last backtick(s)
@timotheecour timotheecour changed the title WIP RFC: universal inline code block syntax for nim doc+rst universal inline code block syntax for nim doc+rst Mar 24, 2021
@narimiran
Copy link
Member

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
Copy link
Member

Araq commented Mar 25, 2021

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

@timotheecour
Copy link
Member Author

timotheecour commented Mar 25, 2021

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:

Use   instead of space characters.
 

And then people copy-past zero-width characters in their code and wonder why it doesn't work... –

Spaces are not allowed as the start or end character in inline code blocks now. Do not include them, or it won't work.

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
Copy link

a-mr commented Mar 26, 2021

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
Copy link
Member Author

timotheecour commented Mar 26, 2021

@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 #355 (comment)).

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

are these too marginal too?

  • rendering trailiing backslash \ in syntax highlighted code
  • rendering backticks in syntax highlighted code (see rst2html)
  • preserving multiple consecutive spaces
  • rendering double/triple backticks

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

  • trivial to understand
  • requires no escaping (WYSIWYG) in particular easy to copy-paste
  • is universal, and has no edge cases
  • is easier to implement (because no escaping needed)

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
Copy link

a-mr commented Mar 27, 2021

@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
Copy link

a-mr commented Mar 27, 2021

A problem with trailing space looks completely artificial.

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

a-mr added a commit to a-mr/Nim that referenced this issue Jul 1, 2022
- add additional parameters parsing (other implementations will just
  ignore them). E.g. if in RST we have:

  .. code:: nim
     :test: "nim c $1"

     ...

  then in Markdown that will be:

  ```nim test="nim c $1"
  ...
  ```

- implement Markdown interpretation of additional indentation which is
  less than 4 spaces (>=4 spaces is a code block but it's not
implemented yet). RST interpretes it as quoted block, for Markdown it's
just normal paragraphs.
- add separate `md2html` and `md2tex` commands. This is to separate
  Markdown behavior in cases when it diverges w.r.t. RST significantly —
most conspicously like in the case of additional indentation above, and
also currently the contradicting inline rule of Markdown is also turned
on only in `md2html` and `md2tex`. **Rationale:** mixing Markdown and
RST arbitrarily is a way to nowhere, we need to provide a way to fix the
particular behavior. Note that still all commands have **both** Markdown
and RST features **enabled**. In this PR `*.nim` files can be processed
only in Markdown mode, while `md2html` is for `*.md` files and
`rst2html` for `*.rst` files.
- rename `*.rst` files to `.*md` as our current default behavior is
  already Markdown-ish
- convert code blocks in `docgen.rst` to Markdown style as an example.
  Other code blocks will be converted in the follow-up PRs
- fix indentation inside Markdown code blocks — additional indentation
  is preserved there
- allow more than 3 backticks open/close blocks (tildas \~ are still not
  allowed to avoid conflict with RST adornment headings) see also
nim-lang/RFCs#355
- better error messages
- (other) fix a bug that admonitions cannot be used in sandbox mode; fix
  annoying warning on line 2711
Araq pushed a commit to nim-lang/Nim that referenced this issue Jul 15, 2022
…19954)

- add additional parameters parsing (other implementations will just
  ignore them). E.g. if in RST we have:

  .. code:: nim
     :test: "nim c $1"

     ...

  then in Markdown that will be:

  ```nim test="nim c $1"
  ...
  ```

- implement Markdown interpretation of additional indentation which is
  less than 4 spaces (>=4 spaces is a code block but it's not
implemented yet). RST interpretes it as quoted block, for Markdown it's
just normal paragraphs.
- add separate `md2html` and `md2tex` commands. This is to separate
  Markdown behavior in cases when it diverges w.r.t. RST significantly —
most conspicously like in the case of additional indentation above, and
also currently the contradicting inline rule of Markdown is also turned
on only in `md2html` and `md2tex`. **Rationale:** mixing Markdown and
RST arbitrarily is a way to nowhere, we need to provide a way to fix the
particular behavior. Note that still all commands have **both** Markdown
and RST features **enabled**. In this PR `*.nim` files can be processed
only in Markdown mode, while `md2html` is for `*.md` files and
`rst2html` for `*.rst` files.
- rename `*.rst` files to `.*md` as our current default behavior is
  already Markdown-ish
- convert code blocks in `docgen.rst` to Markdown style as an example.
  Other code blocks will be converted in the follow-up PRs
- fix indentation inside Markdown code blocks — additional indentation
  is preserved there
- allow more than 3 backticks open/close blocks (tildas \~ are still not
  allowed to avoid conflict with RST adornment headings) see also
nim-lang/RFCs#355
- better error messages
- (other) fix a bug that admonitions cannot be used in sandbox mode; fix
  annoying warning on line 2711
FedericoCeratto pushed a commit to FedericoCeratto/Nim that referenced this issue Jul 30, 2022
…im-lang#19954)

- add additional parameters parsing (other implementations will just
  ignore them). E.g. if in RST we have:

  .. code:: nim
     :test: "nim c $1"

     ...

  then in Markdown that will be:

  ```nim test="nim c $1"
  ...
  ```

- implement Markdown interpretation of additional indentation which is
  less than 4 spaces (>=4 spaces is a code block but it's not
implemented yet). RST interpretes it as quoted block, for Markdown it's
just normal paragraphs.
- add separate `md2html` and `md2tex` commands. This is to separate
  Markdown behavior in cases when it diverges w.r.t. RST significantly —
most conspicously like in the case of additional indentation above, and
also currently the contradicting inline rule of Markdown is also turned
on only in `md2html` and `md2tex`. **Rationale:** mixing Markdown and
RST arbitrarily is a way to nowhere, we need to provide a way to fix the
particular behavior. Note that still all commands have **both** Markdown
and RST features **enabled**. In this PR `*.nim` files can be processed
only in Markdown mode, while `md2html` is for `*.md` files and
`rst2html` for `*.rst` files.
- rename `*.rst` files to `.*md` as our current default behavior is
  already Markdown-ish
- convert code blocks in `docgen.rst` to Markdown style as an example.
  Other code blocks will be converted in the follow-up PRs
- fix indentation inside Markdown code blocks — additional indentation
  is preserved there
- allow more than 3 backticks open/close blocks (tildas \~ are still not
  allowed to avoid conflict with RST adornment headings) see also
nim-lang/RFCs#355
- better error messages
- (other) fix a bug that admonitions cannot be used in sandbox mode; fix
  annoying warning on line 2711
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
…im-lang#19954)

- add additional parameters parsing (other implementations will just
  ignore them). E.g. if in RST we have:

  .. code:: nim
     :test: "nim c $1"

     ...

  then in Markdown that will be:

  ```nim test="nim c $1"
  ...
  ```

- implement Markdown interpretation of additional indentation which is
  less than 4 spaces (>=4 spaces is a code block but it's not
implemented yet). RST interpretes it as quoted block, for Markdown it's
just normal paragraphs.
- add separate `md2html` and `md2tex` commands. This is to separate
  Markdown behavior in cases when it diverges w.r.t. RST significantly —
most conspicously like in the case of additional indentation above, and
also currently the contradicting inline rule of Markdown is also turned
on only in `md2html` and `md2tex`. **Rationale:** mixing Markdown and
RST arbitrarily is a way to nowhere, we need to provide a way to fix the
particular behavior. Note that still all commands have **both** Markdown
and RST features **enabled**. In this PR `*.nim` files can be processed
only in Markdown mode, while `md2html` is for `*.md` files and
`rst2html` for `*.rst` files.
- rename `*.rst` files to `.*md` as our current default behavior is
  already Markdown-ish
- convert code blocks in `docgen.rst` to Markdown style as an example.
  Other code blocks will be converted in the follow-up PRs
- fix indentation inside Markdown code blocks — additional indentation
  is preserved there
- allow more than 3 backticks open/close blocks (tildas \~ are still not
  allowed to avoid conflict with RST adornment headings) see also
nim-lang/RFCs#355
- better error messages
- (other) fix a bug that admonitions cannot be used in sandbox mode; fix
  annoying warning on line 2711
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants