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

Add rstgen.rstToJson #14712

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
- new proc `heapqueue.find[T](heap: HeapQueue[T], x: T): int` to get index of element ``x``.
- Add `rstgen.rstToLatex` convenience proc for `renderRstToOut` and `initRstGenerator` with `outLatex` output.
- Add `os.normalizeExe`, eg: `koch` => `./koch`.
- Add `rstgen.rstToJson` convenience proc for `renderRstToJson` and `rstParse` with JSON output.


## Language changes
Expand Down
8 changes: 8 additions & 0 deletions lib/packages/docutils/rstgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,14 @@ proc rstToLatex*(rstSource: string; options: RstParseOptions): string {.inline,
rstGenera.renderRstToOut(rstParse(rstSource, "", 1, 1, option, options), result)


proc rstToJson*(rstSource: string; options: RstParseOptions): string {.inline, since: (1, 3).} =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • why not use the hook mechanism in jsonutils?
  • badly named since it returns string, not JsonNode; rstToJsonString would be more accurate
  • this doesn't compose; instead, the hook mechanism in jsonutils would allow composition, eg:
type RstSource = distinct string
let b = ("foo", @[RstSource"*hello*", RstSource"**world**"]).toJson

## Convenience proc for `renderRstToJson` and `rstParse`.
runnableExamples: echo rstToJson("*Hello* **world**", {})
Copy link
Member

@timotheecour timotheecour Jun 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use echo, use doAssert ..., otherwise it doesn't tell you how this works

if rstSource.len == 0: return
var option: bool
result = renderRstToJson(rstParse(rstSource, "", 1, 1, option, options))


when isMainModule:
assert rstToHtml("*Hello* **world**!", {},
newStringTable(modeStyleInsensitive)) ==
Expand Down