Skip to content

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

@timotheecour

Description

@timotheecour

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions