package main import ( "bufio" "fmt" "strings" ) type Markup []byte type LineType int8 const ( TextLine LineType = iota HeadingLine UnorderedListItem ) func getLineType(line string) LineType { 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 "No title dtected" } 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 HeadingLine: retval += fmt.Sprintf("\n

%s

", line) case UnorderedListItem: if lastType != UnorderedListItem { retval += "\n" } retval = strings.TrimPrefix(retval, "\n") return retval }