package main import ( "bufio" "fmt" "strings" ) type Markup []byte type LineType int8 const ( TextLine LineType = iota LinkLine HeadingLine UnorderedListItem ) func getLineType(line string) LineType { if strings.HasPrefix(line, "=>") { return LinkLine } if strings.HasPrefix(line, "#") { return HeadingLine } if strings.HasPrefix(line, "* ") { return UnorderedListItem } return TextLine } func (m Markup) title() string { text := string(m) scanner := bufio.NewScanner(strings.NewReader(text)) for scanner.Scan() { line := scanner.Text() if strings.HasPrefix(line, "# ") { return strings.TrimPrefix(line, "# ") } } return "" } func (m Markup) html() string { retval := "" var line string var lineType LineType var lastType LineType = -1 text := string(m) scanner := bufio.NewScanner(strings.NewReader(text)) for scanner.Scan() { line = scanner.Text() lineType = getLineType(line) if lineType != UnorderedListItem && lastType == UnorderedListItem { retval += "\n" } if line == "" { continue } switch lineType { case LinkLine: retval += fmt.Sprintf("\n
%s", line) case HeadingLine: retval += fmt.Sprintf("\n
%s
", line) } lastType = lineType } if lineType == UnorderedListItem { retval += "\n