tables
This commit is contained in:
parent
74d34dee63
commit
5bd96ee4e2
1 changed files with 105 additions and 22 deletions
101
main.go
101
main.go
|
|
@ -80,6 +80,68 @@ func main() {
|
||||||
fmt.Fprint(&out, list2html(line))
|
fmt.Fprint(&out, list2html(line))
|
||||||
|
|
||||||
case rivit.Block:
|
case rivit.Block:
|
||||||
|
if len(line.Body) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
first := strings.TrimSpace(line.Body[0])
|
||||||
|
if len(first) != 0 && first[0] == '|' && first[len(first)-1] == '|' { // table
|
||||||
|
out.WriteString("<table>")
|
||||||
|
|
||||||
|
for i, v := range line.Body {
|
||||||
|
v = strings.TrimSpace(v)
|
||||||
|
if len(v) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if v[0] != '|' {
|
||||||
|
fmt.Fprintf(&out, "<caption>%s</caption>", text2html(rivit.ParseStyledText(v)))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(v, "|")
|
||||||
|
|
||||||
|
if i == 0 {
|
||||||
|
out.WriteString("<thead>")
|
||||||
|
for _, p := range parts {
|
||||||
|
p = strings.TrimSpace(p)
|
||||||
|
if len(p) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
styled := rivit.ParseStyledText(p)
|
||||||
|
fmt.Fprintf(&out, "<th scope=\"col\">%s</th>", text2html(styled))
|
||||||
|
}
|
||||||
|
out.WriteString("</thead>")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if i == 1 {
|
||||||
|
out.WriteString("<tbody>")
|
||||||
|
}
|
||||||
|
|
||||||
|
out.WriteString("<tr>")
|
||||||
|
for it, p := range parts {
|
||||||
|
p = strings.TrimSpace(p)
|
||||||
|
if len(p) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
styled := rivit.ParseStyledText(p)
|
||||||
|
|
||||||
|
if it == 0 {
|
||||||
|
fmt.Fprintf(&out, "<th scope=\"row\">%s</td>", text2html(styled))
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(&out, "<td>%s</td>", text2html(styled))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
out.WriteString("</tr>")
|
||||||
|
}
|
||||||
|
|
||||||
|
out.WriteString("</tbody>")
|
||||||
|
out.WriteString("</table>")
|
||||||
|
} else {
|
||||||
out.WriteString("<pre><code>")
|
out.WriteString("<pre><code>")
|
||||||
|
|
||||||
for i, v := range line.Body {
|
for i, v := range line.Body {
|
||||||
|
|
@ -97,21 +159,19 @@ func main() {
|
||||||
out.WriteString("</code></pre>")
|
out.WriteString("</code></pre>")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
html := fmt.Sprintf(`
|
html := fmt.Sprintf(
|
||||||
<html>
|
`<html>
|
||||||
<head>
|
<head>
|
||||||
<title>%s</title>
|
<title>%s</title>
|
||||||
<style>
|
<style>
|
||||||
html {
|
|
||||||
color-scheme: light dark;
|
|
||||||
}
|
|
||||||
body {
|
body {
|
||||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
|
|
||||||
padding: 0 15px;
|
padding: 0 15px;
|
||||||
max-width: 650px;
|
max-width: 750px;
|
||||||
font-size: 115%%;
|
font-size: 115%%;
|
||||||
line-height: 1.4;
|
line-height: 1.4;
|
||||||
|
margin: 0 auto;
|
||||||
}
|
}
|
||||||
h1, h2 {
|
h1, h2 {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
|
|
@ -127,13 +187,36 @@ func main() {
|
||||||
pre code {
|
pre code {
|
||||||
white-space: pre-wrap;
|
white-space: pre-wrap;
|
||||||
}
|
}
|
||||||
|
table {
|
||||||
|
border-collapse: collapse;
|
||||||
|
width: 100%%;
|
||||||
|
margin-top: 1em;
|
||||||
|
margin-bottom: 1em;
|
||||||
|
}
|
||||||
|
table caption {
|
||||||
|
padding: 0.5em;
|
||||||
|
text-align: left;
|
||||||
|
caption-side: top;
|
||||||
|
background-color: #eee;
|
||||||
|
}
|
||||||
|
th, td {
|
||||||
|
text-align: left;
|
||||||
|
border: none;
|
||||||
|
padding: 0.5rem;
|
||||||
|
}
|
||||||
|
thead th {
|
||||||
|
border-bottom: 2px solid #ddd;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
tbody tr {
|
||||||
|
border-bottom: 1px solid #eee;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
%s
|
%s
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>`, title, out.String())
|
||||||
`, title, out.String())
|
|
||||||
|
|
||||||
if err := os.WriteFile(outpath, []byte(html), 0644); err != nil {
|
if err := os.WriteFile(outpath, []byte(html), 0644); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error writing file: %v\n", err)
|
fmt.Fprintf(os.Stderr, "error writing file: %v\n", err)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue