Source Edit

This module implements an AST for the reStructuredText parser.

Imports

strutils, json

Types

  1. FileIndex = distinct int32

Source Edit

  1. PRstNode = ref RstNode

an RST node Source Edit

  1. RstNode {.acyclic, final.} = object
  2. case kind*: RstNodeKind ## the node's kind
  3. of rnLeaf, rnSmiley:
  4. text*: string ## string that is expected to be displayed
  5. of rnEnumList:
  6. labelFmt*: string ## label format like "(1)"
  7. of rnLineBlockItem:
  8. lineIndent*: string ## a few spaces or newline at the line beginning
  9. of rnAdmonition:
  10. adType*: string ## admonition type: "note", "caution", etc. This
  11. ## text will set the style and also be displayed
  12. of rnOverline, rnHeadline, rnMarkdownHeadline:
  13. level*: int ## level of headings starting from 1 (main
  14. ## chapter) to larger ones (minor sub-sections)
  15. ## level=0 means it's document title or subtitle
  16. of rnFootnote, rnCitation, rnOptionListItem:
  17. order*: int ## footnote order (for auto-symbol footnotes and
  18. ## auto-numbered ones without a label)
  19. of rnMarkdownBlockQuoteItem:
  20. quotationDepth*: int ## number of characters in line prefix
  21. of rnRstRef, rnPandocRef, rnSubstitutionReferences, rnInterpretedText,
  22. rnField, rnInlineCode, rnCodeBlock, rnFootnoteRef:
  23. info*: TLineInfo ## To have line/column info for warnings at
  24. ## nodes that are post-processed after parsing
  25. of rnNimdocRef:
  26. tooltip*: string
  27. of rnTable, rnGridTable, rnMarkdownTable:
  28. colCount*: int ## Number of (not-united) cells in the table
  29. of rnTableRow:
  30. endsHeader*: bool ## Is last row in the header of table?
  31. of rnTableHeaderCell, rnTableDataCell:
  32. span*: int ## Number of table columns that the cell occupies
  33. else:
  34. nil
  35. anchor*: string ## anchor, internal link target
  36. ## (aka HTML id tag, aka Latex label/hypertarget)
  37. sons*: RstNodeSeq ## the node's sons

AST node (result of RST parsing) Source Edit

  1. RstNodeKind = enum
  2. rnInner, rnHeadline, rnOverline, rnMarkdownHeadline, rnTransition,
  3. rnParagraph, rnBulletList, rnBulletItem, rnEnumList, rnEnumItem, rnDefList,
  4. rnMdDefList, rnDefItem, rnDefName, rnDefBody, rnFieldList, rnField,
  5. rnFieldName, rnFieldBody, rnOptionList, rnOptionListItem, rnOptionGroup,
  6. rnOption, rnOptionString, rnOptionArgument, rnDescription, rnLiteralBlock,
  7. rnMarkdownBlockQuote, rnMarkdownBlockQuoteItem, rnLineBlock, rnLineBlockItem,
  8. rnBlockQuote, rnTable, rnGridTable, rnMarkdownTable, rnTableRow,
  9. rnTableHeaderCell, rnTableDataCell, rnFootnote, rnCitation, rnFootnoteGroup,
  10. rnStandaloneHyperlink, rnHyperlink, rnRstRef, rnPandocRef, rnInternalRef,
  11. rnFootnoteRef, rnNimdocRef, rnDirective, rnDirArg, rnRaw, rnTitle, rnContents,
  12. rnImage, rnFigure, rnCodeBlock, rnAdmonition, rnRawHtml, rnRawLatex,
  13. rnContainer, rnIndex, rnSubstitutionDef, rnInlineCode, rnCodeFragment,
  14. rnUnknownRole, rnSub, rnSup, rnIdx, rnEmphasis, rnStrongEmphasis,
  15. rnTripleEmphasis, rnInterpretedText, rnInlineLiteral, rnInlineTarget,
  16. rnSubstitutionReferences, rnSmiley, rnDefaultRole, rnLeaf

the possible node kinds of an PRstNode Source Edit

  1. RstNodeSeq = seq[PRstNode]

Source Edit

  1. TLineInfo = object
  2. line*: uint16
  3. col*: int16
  4. fileIndex*: FileIndex

Source Edit

Procs

  1. proc `==`(a, b: FileIndex): bool {.borrow, ...raises: [], tags: [], forbids: [].}

Source Edit

  1. proc add(father, son: PRstNode) {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc add(father: PRstNode; s: string) {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc addIfNotNil(father, son: PRstNode) {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc lastSon(n: PRstNode): PRstNode {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc len(n: PRstNode): int {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc newRstLeaf(s: string): PRstNode {....raises: [], tags: [], forbids: [].}

Source Edit

  1. proc newRstNode(kind: RstNodeKind; info: TLineInfo; sons: seq[PRstNode] = @[]): PRstNode {.
  2. ...raises: [], tags: [], forbids: [].}

Source Edit

  1. proc newRstNode(kind: RstNodeKind; s: string): PRstNode {....deprecated,
  2. raises: [], tags: [], forbids: [].}

Deprecated

Source Edit

  1. proc newRstNode(kind: RstNodeKind; sons: seq[PRstNode] = @[]; anchor = ""): PRstNode {.
  2. ...raises: [], tags: [], forbids: [].}

Source Edit

  1. proc renderRstToJson(node: PRstNode): string {....raises: [], tags: [], forbids: [].}

Writes the given RST node as JSON that is in the form

  1. {
  2. "kind":string node.kind,
  3. "text":optional string node.text,
  4. "level":optional int node.level,
  5. "sons":optional node array
  6. }

Source Edit

  1. proc renderRstToRst(n: PRstNode; result: var string) {....raises: [Exception],
  2. tags: [RootEffect], forbids: [].}

renders n into its string representation and appends to result. Source Edit

  1. proc renderRstToText(node: PRstNode): string {....raises: [], tags: [], forbids: [].}

minimal text representation of markup node Source Edit

  1. proc treeRepr(node: PRstNode; indent = 0): string {....raises: [], tags: [],
  2. forbids: [].}

Writes the parsed RST node into an AST tree with compact string representation in the format (one line per every sub-node): indent - kind - [text|level|order|adType] - anchor (if non-zero) (suitable for debugging of RST parsing). Source Edit