tables
This commit is contained in:
parent
74d34dee63
commit
5bd96ee4e2
1 changed files with 105 additions and 22 deletions
127
main.go
127
main.go
|
|
@ -80,38 +80,98 @@ func main() {
|
|||
fmt.Fprint(&out, list2html(line))
|
||||
|
||||
case rivit.Block:
|
||||
out.WriteString("<pre><code>")
|
||||
|
||||
for i, v := range line.Body {
|
||||
l := v[line.Indent:]
|
||||
l = strings.ReplaceAll(l, "<", "<")
|
||||
l = strings.ReplaceAll(l, ">", ">")
|
||||
|
||||
out.WriteString(l)
|
||||
|
||||
if i < len(line.Body) {
|
||||
out.WriteString("\n")
|
||||
}
|
||||
if len(line.Body) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
out.WriteString("</code></pre>")
|
||||
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>")
|
||||
|
||||
for i, v := range line.Body {
|
||||
l := v[line.Indent:]
|
||||
l = strings.ReplaceAll(l, "<", "<")
|
||||
l = strings.ReplaceAll(l, ">", ">")
|
||||
|
||||
out.WriteString(l)
|
||||
|
||||
if i < len(line.Body) {
|
||||
out.WriteString("\n")
|
||||
}
|
||||
}
|
||||
|
||||
out.WriteString("</code></pre>")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
html := fmt.Sprintf(`
|
||||
<html>
|
||||
html := fmt.Sprintf(
|
||||
`<html>
|
||||
<head>
|
||||
<title>%s</title>
|
||||
<style>
|
||||
html {
|
||||
color-scheme: light dark;
|
||||
}
|
||||
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;
|
||||
max-width: 650px;
|
||||
max-width: 750px;
|
||||
font-size: 115%%;
|
||||
line-height: 1.4;
|
||||
margin: 0 auto;
|
||||
}
|
||||
h1, h2 {
|
||||
font-weight: bold;
|
||||
|
|
@ -127,13 +187,36 @@ func main() {
|
|||
pre code {
|
||||
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>
|
||||
</head>
|
||||
<body>
|
||||
%s
|
||||
</body>
|
||||
</html>
|
||||
`, title, out.String())
|
||||
</html>`, title, out.String())
|
||||
|
||||
if err := os.WriteFile(outpath, []byte(html), 0644); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error writing file: %v\n", err)
|
||||
|
|
|
|||
Loading…
Reference in a new issue