~xigoi/xidoc

aadb632b64afc1c22bc1367d455fa21b165df9b4 — Adam Blažek 2 months ago 84703e7
document-class setting and \ command
4 files changed, 40 insertions(+), 21 deletions(-)

M docs/commands.html
M docs/commands.xd
M src/xidoc.nim
M src/xidocpkg/commands/default.nim
M docs/commands.html => docs/commands.html +5 -2
@@ 245,8 245,9 @@ font-family:var(--sans-serif);}code[class*=language-],pre[class*=language-]{font
<section id="set-title" class="command"><h4 class="xd-section-heading"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">set-title</span> title<span class="token punctuation">]</span></code></h4>Sets the given <code>title</code> as the title of the document.</section>
<hr />
<p>The following is a list of possible keys to <a href="#set"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">set</span><span class="token punctuation">]</span></code></a>.</p>
<section class="command"><h4 class="xd-section-heading"><code>katex-stylesheet-path</code></h4>Sets the path for locating the KaTeX font files. See the Math section for more information.</section>
<section class="command"><h4 class="xd-section-heading"><code>syntax-highlighting-theme</code></h4>Sets the theme for syntax highlighting with the <a href="#code"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">code</span><span class="token punctuation">]</span></code></a> and <a href="#code-block"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">code-block</span><span class="token punctuation">]</span></code></a> commands. The available themes are <code>default</code>, <code>dark</code>, <code>funky</code>, <code>okaidia</code>, <code>twilight</code>, <code>coy</code>, <code>solarized-light</code>, and <code>tomorrow-night</code>. You can try out these themes on <a href="https://prismjs.com/">the Prism website</a>. There is also <code>funky-x</code>, a modification of <code>funky</code> with a black background instead of weird stripes.</section></section>
<section class="command"><h4 class="xd-section-heading"><code>document-class</code></h4><p><b>Default value:</b> <code>article</code></p>Only applies to <span class="xd-latex">L<sup>a</sup>T<sub>e</sub>X</span>. Sets the document class in the header.</section>
<section class="command"><h4 class="xd-section-heading"><code>katex-stylesheet-path</code></h4><p><b>Default value:</b> <code>https://cdn.jsdelivr.net/npm/katex@0.13.18/dist/katex.min.css</code></p>Sets the path for locating the KaTeX font files. See the Math section for more information.</section>
<section class="command"><h4 class="xd-section-heading"><code>syntax-highlighting-theme</code></h4><p><b>Default value:</b> <code>default</code></p>Sets the theme for syntax highlighting with the <a href="#code"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">code</span><span class="token punctuation">]</span></code></a> and <a href="#code-block"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">code-block</span><span class="token punctuation">]</span></code></a> commands. The available themes are <code>default</code>, <code>dark</code>, <code>funky</code>, <code>okaidia</code>, <code>twilight</code>, <code>coy</code>, <code>solarized-light</code>, and <code>tomorrow-night</code>. You can try out these themes on <a href="https://prismjs.com/">the Prism website</a>. There is also <code>funky-x</code>, a modification of <code>funky</code> with a black background instead of weird stripes.</section></section>
<section><h3 class="xd-section-heading">Custom commands</h3><section id="def" class="command"><h4 class="xd-section-heading"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">def</span> name<span class="token punctuation">;</span> ?params<span class="token punctuation">;</span> body<span class="token punctuation">]</span></code></h4>Defines a command with the given <code>name</code> that expands to <code>body</code>. If <code>params</code>, which should be space-separated words, are given, the command can take arguments, which can be accessed using the <a href="#arg"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">arg</span><span class="token punctuation">]</span></code></a> command and its variants (<a href="#arg-expand"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">arg-expand</span><span class="token punctuation">]</span></code></a>, <a href="#arg-raw"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">arg-raw</span><span class="token punctuation">]</span></code></a>, <a href="#arg-raw-escape"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">arg-raw-escape</span><span class="token punctuation">]</span></code></a>). The command will only be visible in the scope where it was defined; if you want it to be visible everywhere, use <a href="#def-global"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">def-global</span><span class="token punctuation">]</span></code></a>.
<table><tr><th>Example</th><th>Output</th></tr>
<tr><td><pre class="language-xidoc"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">def</span> greet<span class="token punctuation">;</span> name<span class="token punctuation">;</span> Hello, <span class="token punctuation">[</span><span class="token tag">arg</span> name<span class="token punctuation">]</span>!<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token tag">greet</span> reader<span class="token punctuation">]</span></code></pre></td><td><div>Hello, reader!</div></td></tr></table>


@@ 322,6 323,8 @@ font-family:var(--sans-serif);}code[class*=language-],pre[class*=language-]{font
<table><tr><th>Example</th><th>Output</th></tr>
<tr><td><pre class="language-xidoc"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">style</span> <span class="token punctuation">[</span><span class="token tag">rule</span> .golden-frame<span class="token punctuation">;</span> <span class="token punctuation">[</span><span class="token tag">:</span> border<span class="token punctuation">;</span> 3px solid gold<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token tag">&lt;div></span> .golden-frame<span class="token punctuation">;</span> This text is a work of art!<span class="token punctuation">]</span></code></pre></td><td><div><div class="golden-frame">This text is a work of art!</div></div></td></tr></table></section>
<section id="empty-favicon" class="command"><h4 class="xd-section-heading"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">empty-favicon</span><span class="token punctuation">]</span></code></h4>Specifies the favicon of the webpage to be empty. This prevents unnecessary requests to <code>favicon.ico</code>.</section></section>
<section><h3 class="xd-section-heading"><span class="xd-latex">L<sup>a</sup>T<sub>e</sub>X</span>-specific</h3><section id="\" class="command"><h4 class="xd-section-heading"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">\</span> command *arguments<span class="token punctuation">]</span></code></h4>Uses the given <span class="xd-latex">L<sup>a</sup>T<sub>e</sub>X</span> <code>command</code> with the given <code>arguments</code>.
</section></section>
<section><h3 class="xd-section-heading">Other top-level commands</h3><section id="draw" class="command"><h4 class="xd-section-heading"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">draw</span> ?width<span class="token punctuation">;</span> ?height<span class="token punctuation">;</span> description<span class="token punctuation">]</span></code></h4><span style="color:red">[Experimental]</span> Draws a vector image with the given dimensions based on the <code>description</code>. The <code>description</code> format won't be documented until it's stabilized.
<table><tr><th>Example</th><th>Output</th></tr>
<tr><td><pre class="language-xidoc"><code class="language-xidoc"><span class="token punctuation">[</span><span class="token tag">draw</span> 80<span class="token punctuation">;</span> 80<span class="token punctuation">;</span>

M docs/commands.xd => docs/commands.xd +14 -4
@@ 382,13 382,16 @@ const factorial = (n) => {
    ]
    [<hr>]
    [p The following is a list of possible keys to [cmd-ref set].]
    [def setting; name description;
      [html-add-attrs .command; [section [ms [arg name]]; [arg description]]]
    [def setting; name default-value description;
      [html-add-attrs .command; [section [ms [arg name]]; [p [bf Default value:] [ms [arg default-value]]][arg description]]]
    ]
    [setting katex-stylesheet-path;
    [setting document-class; article;
      Only applies to [LaTeX]. Sets the document class in the header.
    ]
    [setting katex-stylesheet-path; https://cdn.jsdelivr.net/npm/katex@0.13.18/dist/katex.min.css;
      Sets the path for locating the KaTeX font files. See the Math section for more information.
    ]
    [setting syntax-highlighting-theme;
    [setting syntax-highlighting-theme; default;
      Sets the theme for syntax highlighting with the [cmd-ref code] and [cmd-ref code-block] commands. The available themes are [ms default], [ms dark], [ms funky], [ms okaidia], [ms twilight], [ms coy], [ms solarized-light], and [ms tomorrow-night]. You can try out these themes on [link the Prism website; https://prismjs.com/]. There is also [ms funky-x], a modification of [ms funky] with a black background instead of weird stripes.
    ]
  ]


@@ 523,6 526,13 @@ const factorial = (n) => {
    ]
  ]

  [section [LaTeX]-specific;
    [cmd \; command *arguments;
      Uses the given [LaTeX] [param command] with the given [param arguments].
      [if-latex [ex [\ textbf; This is a bold statement]]]
    ]
  ]

  [section Other top-level commands;
    [cmd draw; ?width[;] ?height[;] description;
      [experimental] Draws a vector image with the given dimensions based on the [param description]. The [param description] format won't be documented until it's stabilized.

M src/xidoc.nim => src/xidoc.nim +20 -14
@@ 2,8 2,11 @@ from std/htmlgen as htg import nil
import std/options
import std/sequtils
import std/sets
import std/strformat
import std/strutils
import std/tables
import xidocpkg/commands/default
import xidocpkg/commands/utils
import xidocpkg/error
import xidocpkg/expand
import xidocpkg/translations


@@ 16,11 19,6 @@ const extensions = [
  tLatex: "tex",
  tGemtext: "gmi",
]
const templates = [
  tHtml: """<!DOCTYPE html><html lang="$3"><head><meta charset="utf-8"><meta name="generator" content="xidoc"><meta name="viewport" content="width=device-width,initial-scale=1">$1</head><body>$2</body></html>""",
  tLatex: """\documentclass{article}\usepackage[utf8]{inputenc}\usepackage[$3]{babel}\usepackage{geometry}$1\begin{document}$2\end{document}""",
  tGemtext: "$1$2",
]

proc renderXidoc*(body: string, path = "", target = tHtml, snippet = false, safeMode = false, verbose = false, colorfulError = false): string =
  let doc = Document(


@@ 45,15 43,23 @@ proc renderXidoc*(body: string, path = "", target = tHtml, snippet = false, safe
  else:
    if doc.target == tHtml and doc.addToStyle.len != 0:
      doc.addToHead.incl htg.style(doc.addToStyle.toSeq.join)
    let languageString =
      case target
      of tHtml:
        translate(pHtmlLanguageCode, doc.lookup(lang))
      of tLatex:
        translate(pLatexLanguageName, doc.lookup(lang))
      else:
        ""
    return templates[target] % [doc.addToHead.toSeq.join, rendered, languageString]
    let head = doc.addToHead.toSeq.join
    case doc.target
    of tHtml:
      let lang = translate(pHtmlLanguageCode, doc.lookup(lang))
      &"""<!DOCTYPE html><html lang="{lang}"><head><meta charset="utf-8"><meta name="generator" content="xidoc"><meta name="viewport" content="width=device-width,initial-scale=1">{head}</head><body>{rendered}</body></html>"""
    of tLatex:
      let lang = translate(pLatexLanguageName, doc.lookup(lang))
      "documentclass"{doc.settings.getOrDefault("document-class", "article")} &
      "usepackage"["utf8"]{"inputenc"} &
      "usepackage"[lang]{"babel"} &
      "usepackage"{"geometry"} &
      head &
      "begin"{"document"} &
      rendered &
      "end"{"document"}
    of tGemtext:
      &"{head}{rendered}"

when isMainModule and not defined(js):
  import cligen

M src/xidocpkg/commands/default.nim => src/xidocpkg/commands/default.nim +1 -1
@@ 813,7 813,7 @@ commands defaultCommands:
  of tLatex:

    proc backslashCmd(command: !String, args: *Markup): Markup {.command: "\\".} =
      "\\" & args.mapIt("{$1}" % it).join
      "\\" & command & args.mapIt("{$1}" % it).join

  else:
    discard