commit cfcd2ed6eb61656a8fcf75e417336624c7f26a8e Author: m15o Date: Tue Nov 2 06:28:59 2021 +0100 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dd05eb9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +bin +*~ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..867476b --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +build: + CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -o bin/ni main.go diff --git a/gmi2html/convert.go b/gmi2html/convert.go new file mode 100644 index 0000000..59fd38f --- /dev/null +++ b/gmi2html/convert.go @@ -0,0 +1,114 @@ +package gmi2html + +import ( + "html" + "regexp" + "strings" +) + +var heading1Regexp = regexp.MustCompile("^# (.*)$") +var heading2Regexp = regexp.MustCompile("^## (.*)$") +var heading3Regexp = regexp.MustCompile("^### (.*)$") +var linkRegexp = regexp.MustCompile("^=> ([^\\s]+) ?(.+)?$") +var blockquoteRegexp = regexp.MustCompile("^> (.*)$") +var preRegexp = regexp.MustCompile("^```.*$") +var bulletRegexp = regexp.MustCompile(`^\* ?(.*)$`) + +func clearLinkMode(linkMode *bool, rv *[]string) { + if *linkMode { + *rv = append(*rv, "

") + *linkMode = false + } +} + +func clearUlMode(ulMode *bool, rv *[]string) { + if *ulMode { + *rv = append(*rv, "") + *ulMode = false + } +} + +func sanitize(input string) string { + return html.EscapeString(input) +} + +func Convert(gmi string) string { + var rv []string + preMode := false + ulMode := false + linkMode := false + for _, l := range strings.Split(gmi, "\n") { + l = strings.TrimRight(l, "\r") + if preMode { + switch { + case preRegexp.MatchString(l): + rv = append(rv, "") + preMode = false + default: + rv = append(rv, sanitize(l)) + } + } else { + switch { + case heading1Regexp.MatchString(l): + clearUlMode(&ulMode, &rv) + clearLinkMode(&linkMode, &rv) + matches := heading1Regexp.FindStringSubmatch(l) + rv = append(rv, "

"+sanitize(matches[1])+"

") + case heading2Regexp.MatchString(l): + clearUlMode(&ulMode, &rv) + clearLinkMode(&linkMode, &rv) + matches := heading2Regexp.FindStringSubmatch(l) + rv = append(rv, "

"+sanitize(matches[1])+"

") + case heading3Regexp.MatchString(l): + clearUlMode(&ulMode, &rv) + clearLinkMode(&linkMode, &rv) + matches := heading3Regexp.FindStringSubmatch(l) + rv = append(rv, "

"+sanitize(matches[1])+"

") + case blockquoteRegexp.MatchString(l): + clearUlMode(&ulMode, &rv) + clearLinkMode(&linkMode, &rv) + matches := blockquoteRegexp.FindStringSubmatch(l) + rv = append(rv, "
"+sanitize(matches[1])+"
") + case linkRegexp.MatchString(l): + clearUlMode(&ulMode, &rv) + matches := linkRegexp.FindStringSubmatch(l) + if len(matches[2]) == 0 { + matches[2] = matches[1] + } + if strings.HasSuffix(matches[1], ".png") || strings.HasSuffix(matches[1], ".PNG") || strings.HasSuffix(matches[1], ".jpg") || strings.HasSuffix(matches[1], ".JPG") || strings.HasSuffix(matches[1], ".jpeg") || strings.HasSuffix(matches[1], ".gif") || strings.HasSuffix(matches[1], ".GIF") { + rv = append(rv, "") + continue + } + if linkMode { + rv = append(rv, ""+sanitize(matches[2])+"
") + continue + } + rv = append(rv, "

"+sanitize(matches[2])+"
") + linkMode = true + case preRegexp.MatchString(l): + clearUlMode(&ulMode, &rv) + clearLinkMode(&linkMode, &rv) + rv = append(rv, "

")
+				preMode = true
+			case bulletRegexp.MatchString(l):
+				clearLinkMode(&linkMode, &rv)
+				matches := bulletRegexp.FindStringSubmatch(l)
+				if ulMode {
+					rv = append(rv, "
  • "+sanitize(matches[1])+"
  • ") + continue + } + rv = append(rv, "