d2/d2renderers/d2svg/code.go

72 lines
1.7 KiB
Go
Raw Normal View History

package d2svg
import (
"strings"
2023-03-04 04:08:13 +00:00
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/formatters/svg"
)
// Copied private functions from chroma. Their public functions do too much (write the whole SVG document)
// https://github.com/alecthomas/chroma
// >>> BEGIN
var svgEscaper = strings.NewReplacer(
`&`, "&",
`<`, "&lt;",
`>`, "&gt;",
`"`, "&quot;",
` `, "&#160;",
` `, "&#160;&#160;&#160;&#160;",
)
func styleToSVG(style *chroma.Style) map[chroma.TokenType]string {
converted := map[chroma.TokenType]string{}
2023-03-04 05:02:02 +00:00
// NOTE this is in the original source code, but it just makes unhighlightable code turn into the bg color
// Which I don't understand, and I get the results I want when I remove it.
// bg := style.Get(chroma.Background)
for t := range chroma.StandardTypes {
entry := style.Get(t)
2023-03-04 05:02:02 +00:00
// if t != chroma.Background {
// entry = entry.Sub(bg)
// }
if entry.IsZero() {
continue
}
converted[t] = svg.StyleEntryToSVG(entry)
}
return converted
}
func styleAttr(styles map[chroma.TokenType]string, tt chroma.TokenType) string {
if _, ok := styles[tt]; !ok {
tt = tt.SubCategory()
if _, ok := styles[tt]; !ok {
tt = tt.Category()
if _, ok := styles[tt]; !ok {
return ""
}
}
}
2023-03-04 05:02:02 +00:00
// Custom code
out := styles[tt]
classes := []string{}
if strings.Contains(out, `font-weight="bold"`) {
out = strings.Replace(out, `font-weight="bold"`, ``, 1)
classes = append(classes, "text-mono-bold")
}
if strings.Contains(out, `font-style="italic"`) {
out = strings.Replace(out, `font-style="italic"`, ``, 1)
classes = append(classes, "text-mono-italic")
}
if len(classes) > 0 {
out += `class="` + strings.Join(classes, " ") + `"`
}
return strings.TrimSpace(out)
}
// <<< END