Franck Pommereau

improved doc generation

import io, inspect
import io, inspect, sys, pathlib, inspect, subprocess, re
from .cpuinfo import description, parameters, extra, numdescription, regdoc, regname
from .asm import stdlib, Preprocessor
......@@ -85,11 +85,84 @@ def _prepro () :
if not doc :
continue
usage, desc = doc.split("\n", 1)
out.write(f"## %{usage}\n\n{desc}\n\n")
out.write(f"## `%{usage}`\n\n{desc}\n\n")
return out.getvalue().strip()
def _clean_line (tex) :
for old, new in [(r"\textquotesingle{}", '""'),
#(r"\#", "#"),
#(r"\%", "%"),
(r"\ ", " "),
("{[}", "["),
("{]}", "]"),
#(r"\$", "$"),
(r"\textless\textless{}", r"\texttt{<<}"),
(r"\textgreater\textgreater{}", r"\texttt{>>}"),
("\u27a1\ufe0f", r"$\mapsto$"),
("\u25b6\ufe0f", r"$\rightarrow$"),
(r"\texttt", r"\ttcsrc"),
#("{verbatim}", "{ttcsource}"),
(r"\section", r"\ttcsection"),
(r"\subsection", r"\ttcsubsection"),
] :
tex = tex.replace(old, new)
tex = re.sub(r"\\texorpdfstring\{\\ttcsrc\{([^\{\}]*)\}\}\{\1\}", r"\\ttcsrc{\1}", tex)
return tex
def _clean_lines (tex) :
trim = False
for line in tex.splitlines() :
if line.startswith(r"\hypertarget") :
trim = True
elif trim :
trim = False
yield line.split(r"\label")[0]
elif line == r"\tightlist" :
pass
else :
yield line
def clean_tex (tex) :
tex = "\n".join(map(_clean_line, _clean_lines(tex)))
return tex
if __name__ == "__main__" :
print(help.format(regdoc=_regdoc(),
import argparse
parser = argparse.ArgumentParser(prog="ttc._help", description="TTC help generator")
parser.add_argument("-o", dest="output", metavar="PATH",
default=None, type=str,
help="output help to PATH (default: stdout)")
parser.add_argument("-f", dest="format", metavar="FMT",
default=None, type=lambda s : str(s).lower(),
help="generate doc as FMT (default: 'md')")
args = parser.parse_args()
if args.format is None :
if args.output in ("-", None) :
args.format = "md"
else :
args.format = pathlib.Path(args.output).suffix.lstrip(".").lower()
doc = help.format(regdoc=_regdoc(),
instrdoc=_instrdoc(),
stdlib=_stdlib(),
prepro=_prepro()).strip())
prepro=_prepro()).strip() +"\n"
if args.format == "md" :
pass
elif args.format == "tex" :
doc = subprocess.check_output(["pandoc", "-t", "latex"],
input=doc, encoding="utf-8")
doc = clean_tex(doc)
elif args.format == "html" :
doc = subprocess.check_output(["pandoc", "-t", "html"],
input=doc, encoding="utf-8")
else :
parser.exit(1, inspect.cleandoc("""unsupported format {args.format!r}
use one of
- md: Markdown
- tex: LaTeX
- html: HTML
""") + "\n")
if args.output in ("-", None) :
out = sys.stdout
else :
out = open(args.output, "w", encoding="utf-8")
out.write(doc)
......
......@@ -162,6 +162,12 @@ html, body {
margin-top: 5px;
}
.help h2 code {
background-color: #fff;
border: 0px;
padding: 0px;
}
.help ul {
margin: 0px;
padding-left: 20px;
......
......@@ -146,11 +146,11 @@
# Preprocessor
## %alias NAME REPL
## `%alias NAME REPL`
define `$NAME` to be replaced by `REPL` for op/macros arguments
## %def NAME ARG...
## `%def NAME ARG...`
define a macro `NAME`
```
......@@ -161,7 +161,7 @@ if necessary
%end
```
## %ifalias NAME
## `%ifalias NAME`
insert text if an alias `NAME` is defined
```
......@@ -173,7 +173,7 @@ this part is optional
%endif
```
## %ifdef NAME
## `%ifdef NAME`
insert text if an op/macro `NAME` is defined
```
......@@ -185,7 +185,7 @@ this part is optional
%endif
```
## %ifnalias NAME
## `%ifnalias NAME`
insert text if an alias `NAME` is not defined
```
......@@ -197,7 +197,7 @@ this part is optional
%endif
```
## %ifndef NAME
## `%ifndef NAME`
insert text if an op/macro `NAME` is not defined
```
......@@ -209,7 +209,7 @@ this part is optional
%endif
```
## %ifnnum VALUE
## `%ifnnum VALUE`
insert text if `VALUE` is not a number (or label value)
```
......@@ -221,7 +221,7 @@ this part is optional
%endif
```
## %ifnreg VALUE
## `%ifnreg VALUE`
insert text if `VALUE` is not a register
```
......@@ -233,7 +233,7 @@ this part is optional
%endif
```
## %ifnum VALUE
## `%ifnum VALUE`
insert text if `VALUE` is a number (or label value)
```
......@@ -245,7 +245,7 @@ this part is optional
%endif
```
## %ifreg VALUE
## `%ifreg VALUE`
insert text if `VALUE` is a register
```
......@@ -257,10 +257,10 @@ this part is optional
%endif
```
## %unalias NAME
## `%unalias NAME`
undefine previously defined `$NAME`
## %undef NAME
## `%undef NAME`
undefine a macro `NAME`
......