Skip to content

Commit

Permalink
use shortcode for stdlib functions instead of generated html
Browse files Browse the repository at this point in the history
  • Loading branch information
NotLe0n committed Dec 3, 2023
1 parent f6650f6 commit 7cf93bd
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 67 deletions.
97 changes: 32 additions & 65 deletions gen/stdlib-generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"bytes"
"fmt"
"html"
"io/fs"
"os"
"path/filepath"
Expand All @@ -18,24 +17,12 @@ import (

var nameMap = map[string]map[string]string{
"DE": {
"params": "Parameter",
"paramType": "Parameter Typ",
"returnType": "Rückgabe Typ",
"aliases": "Aliase",
"impl": "Implementation",
"externImpl": "Implementiert in",
"type": "Typ",
"var": "Variablen",
"func": "Funktionen",
"moduleEmpty": "Dieses Modul ist Leer",
},
"EN": {
"params": "Parameters",
"paramType": "Parameter type",
"returnType": "Return type",
"aliases": "Aliases",
"impl": "Implementation",
"externImpl": "Implemented in",
"type": "Type",
"var": "variables",
"func": "functions",
Expand Down Expand Up @@ -128,74 +115,54 @@ func MakeMdFiles(inputFilePath, outputFilePath, lang string) {
case *ast.FuncDecl:
hasFuncs = true

fmt.Fprintln(funcBldr, "<details>")
// Funktionsname
fmt.Fprintf(funcBldr, "<summary><h2>%s</h2></summary>\n", decl.Name())

fmt.Fprintln(funcBldr, "<ul>")
// Kommentar/Beschreibung
descr := ""
if decl.Comment() != nil {
descr := strings.Replace(strings.Trim(decl.Comment().String(), "[] \r\n"), "\t", "", -1)
fmt.Fprintf(funcBldr, "<pre>\n%s\n</pre>\n", descr)
descr = strings.Replace(strings.Trim(decl.Comment().String(), "[] \r\n"), "\t", "", -1)
descr = strings.ReplaceAll(descr, "\r", "")
descr = strings.ReplaceAll(descr, "\n", "<br>")
descr = strings.ReplaceAll(descr, "\"", "\\\"")
}

params := ""
paramTypes := ""
if len(decl.ParamNames) > 0 {
// Parameter Names
fmt.Fprintf(funcBldr, "\t<li>%s: ", nameMap[lang]["params"])
for i, paramName := range decl.ParamNames {
fmt.Fprintf(funcBldr, "<code>%s</code>", paramName.String())
if i < len(decl.ParamNames)-1 {
fmt.Fprintf(funcBldr, ", ")
}
}

// Parameter Types
fmt.Fprintf(funcBldr, "</li>\n\t<li>%s", nameMap[lang]["paramType"])
if len(decl.ParamNames) > 1 {
if lang == "DE" {
fmt.Fprintf(funcBldr, "en")
} else if lang == "EN" {
fmt.Fprintf(funcBldr, "s")
}
names := make([]string, 0, len(decl.ParamNames))
types := make([]string, 0, len(decl.ParamTypes))
for i := range decl.ParamNames {
names = append(names, decl.ParamNames[i].String())
types = append(types, decl.ParamTypes[i].String())
}
fmt.Fprintf(funcBldr, ": ")

for i, paramTypes := range decl.ParamTypes {
fmt.Fprintf(funcBldr, "<code>%s</code>", paramTypes)
if i < len(decl.ParamTypes)-1 {
fmt.Fprintf(funcBldr, ", ")
}
}
params = strings.Join(names, ",")
paramTypes = strings.Join(types, ",")
}

// Rückgabe Typ
fmt.Fprintf(funcBldr, "</li>\n\t<li>%s: <code>%s</code></li>\n", nameMap[lang]["returnType"], decl.Type)
fmt.Fprintln(funcBldr, "</ul>")

// Aliases
fmt.Fprintf(funcBldr, "\n<h3>%s</h3>\n<ol>\n", nameMap[lang]["aliases"])
for _, alias := range decl.Aliases {
fmt.Fprintf(funcBldr, "\t<li><code>%s</code></li>\n", html.EscapeString(alias.Original.Literal))
aliases := ""
for i, alias := range decl.Aliases {
aliases += strings.Trim(alias.Original.Literal, "\"\n")
if i+1 < len(decl.Aliases) {
aliases += "\\\""
}
}
fmt.Fprintln(funcBldr, "</ol>")

// Implemetation
fmt.Fprintf(funcBldr, "\n<h3>%s</h3>\n", nameMap[lang]["impl"])
impl := ""
isExtern := ast.IsExternFunc(decl)
if ast.IsExternFunc(decl) {
fmt.Fprintf(funcBldr, "%s <code>%s</code>\n", nameMap[lang]["externImpl"], decl.ExternFile.String())
impl = strings.Trim(decl.ExternFile.String(), "\"")
} else {
fmt.Fprintln(funcBldr, "<pre class=\"language-ddp\" tabindex=\"0\">\n<code class=\"language-ddp\">")

inputStr := strings.Split(string(inputFile), "\n")
file := strings.Trim(string(inputFile), "\r\n")
inputStr := strings.Split(file, "\n")
lines := inputStr[decl.Body.Range.Start.Line:decl.Body.Range.End.Line]
for _, line := range lines {
fmt.Fprintln(funcBldr, strings.Replace(line, "\t", "", 1))
}

fmt.Fprintln(funcBldr, "\n</code>\n</pre>")
impl = strings.Join(lines, "<br>")
impl = strings.ReplaceAll(impl, "\r", "")
impl = strings.ReplaceAll(impl, "\n", "<br>")
impl = strings.ReplaceAll(impl, "\"", "\\\"")
impl = strings.ReplaceAll(impl, "\t", "&emsp;")
}

fmt.Fprint(funcBldr, "</details>\n\n")
fmt.Fprintf(funcBldr, "{{< duden-function name=\"%s\" desc=\"%s\" params=\"%s\" paramTypes=\"%s\" ret=\"%s\" impl=\"%s\" extern=\"%t\" aliases=\"%s\" >}}\n\n", // }}"
decl.Name(), descr, params, paramTypes, decl.Type.String(), impl, isExtern, aliases)
case *ast.VarDecl:
hasVars = true

Expand Down
9 changes: 8 additions & 1 deletion i18n/de.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
toc = "Inhaltsverzeichnis"
prev = "Vorherige Seite"
next = "Nächste Seite"
desc = "Die Dokumentation der Deutschen Programmiersprache."
desc = "Die Dokumentation der Deutschen Programmiersprache."
params = "Parameter"
paramType = "Parameter Typ"
returnType = "Rückgabe Typ"
aliases = "Aliase"
impl = "Implementation"
externImpl = "Implementiert in"
type = "Typ"
9 changes: 8 additions & 1 deletion i18n/en.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
toc = "Table of Contents"
prev = "Previous Page"
next = "Next Page"
desc = "The documentation for DDP."
desc = "The documentation for DDP."
params = "Parameters"
paramType = "Parameter type"
returnType = "Return type"
aliases = "Aliases"
impl = "Implementation"
externImpl = "Implemented in"
type = "Type"
41 changes: 41 additions & 0 deletions layouts/shortcodes/duden-function.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<details>
<summary><h2>{{ .Get "name" }}</h2></summary>
{{ with .Get "desc" }}
<pre>{{ . | plainify }}</pre>
{{ end }}
<ul>
{{ with .Get "params" }}
<li>{{ i18n "params"}}:
{{ $params := strings.Split . "," }}
<code>{{ delimit $params "</code>, <code>" }}</code>
</li>
{{ end }}
{{ with .Get "paramTypes" }}
<li>{{ i18n "paramType"}}:
{{ $params := strings.Split . "," }}
<code>{{ delimit $params "</code>, <code>" }}</code>
</li>
{{ end }}
<li>{{ i18n "returnType"}}: <code>{{ .Get "ret" }}</code></li>
</ul>

<h3>{{ i18n "aliases" }}</h3>
<ol>
{{ with .Get "aliases" }}
{{ $aliases := split . "\"" }} <!--"}}-->
{{ range $aliases}}
<li><code>{{ . }}</code></li>
{{ end }}
{{ else}}
<p>Keine Aliase definiert</p>
{{ end}}
</ol>

<h3>{{ i18n "impl" }}</h3>
{{ if eq (.Get "extern") "false" }}
<pre class="language-ddp" tabindex="0"><code class="language-ddp">{{ replace (.Get "impl" | plainify) "&emsp;" " " }}</code></pre>
{{ else }}
<p>{{ i18n "externImpl" }} <code>{{ .Get "impl" }}</code></p>
{{ end }}

</details>

0 comments on commit 7cf93bd

Please sign in to comment.