File: util.go

package info (click to toggle)
golang-github-niklasfasching-go-org 1.6.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 836 kB
  • sloc: sh: 142; makefile: 42
file content (78 lines) | stat: -rw-r--r-- 2,033 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package blorg

import (
	"reflect"
	"regexp"
	"strings"

	"github.com/alecthomas/chroma/v2"
	"github.com/alecthomas/chroma/v2/formatters/html"
	"github.com/alecthomas/chroma/v2/lexers"
	"github.com/alecthomas/chroma/v2/styles"
	"github.com/niklasfasching/go-org/org"
)

var snakeCaseRegexp = regexp.MustCompile(`(^[A-Za-z])|_([A-Za-z])`)
var whitespaceRegexp = regexp.MustCompile(`\s+`)
var nonWordCharRegexp = regexp.MustCompile(`[^\w-]`)

func toMap(bufferSettings map[string]string, x interface{}) map[string]interface{} {
	m := map[string]interface{}{}
	for k, v := range bufferSettings {
		k = toCamelCase(k)
		if strings.HasSuffix(k, "[]") {
			m[k[:len(k)-2]] = strings.Fields(v)
		} else {
			m[k] = v
		}
	}
	if x == nil {
		return m
	}
	v := reflect.ValueOf(x).Elem()
	for i := 0; i < v.NumField(); i++ {
		m[v.Type().Field(i).Name] = v.Field(i).Interface()
	}
	return m
}

func toCamelCase(s string) string {
	return snakeCaseRegexp.ReplaceAllStringFunc(strings.ToLower(s), func(s string) string {
		return strings.ToUpper(strings.Replace(s, "_", "", -1))
	})
}

func slugify(s string) string {
	s = strings.ToLower(s)
	s = whitespaceRegexp.ReplaceAllString(s, "-")
	s = nonWordCharRegexp.ReplaceAllString(s, "")
	return strings.Trim(s, "-")
}

func getWriter() org.Writer {
	w := org.NewHTMLWriter()
	w.HighlightCodeBlock = highlightCodeBlock
	return w
}

func highlightCodeBlock(source, lang string, inline bool, params map[string]string) string {
	var w strings.Builder
	l := lexers.Get(lang)
	if l == nil {
		l = lexers.Fallback
	}
	l = chroma.Coalesce(l)
	it, _ := l.Tokenise(nil, source)
	options := []html.Option{}
	if params[":hl_lines"] != "" {
		ranges := org.ParseRanges(params[":hl_lines"])
		if ranges != nil {
			options = append(options, html.HighlightLines(ranges))
		}
	}
	_ = html.New(options...).Format(&w, styles.Get("github"), it)
	if inline {
		return `<div class="highlight-inline">` + "\n" + w.String() + "\n" + `</div>`
	}
	return `<div class="highlight">` + "\n" + w.String() + "\n" + `</div>`
}