Мария обнови решението на 03.12.2013 01:36 (преди над 4 години)
+package main
+
+import (
+ "regexp"
+ "strconv"
+ "strings"
+)
+
+type MarkdownParser struct {
+ Text string
+}
+
+func NewMarkdownParser(text string) *MarkdownParser {
+ mp := new(MarkdownParser)
+ mp.Text = text
+ return mp
+}
+
+func removeLeadingAndTrailingSymbols(symbols []string, src string) string {
+ reLeading := strings.Join([]string{symbols[0], `|\n`, symbols[0]}, "")
+ reTrailing := strings.Join([]string{`\n[`, symbols[1], `]+`}, "")
+ leading := regexp.MustCompile(reLeading).Split(src, 2)
+ trailing := regexp.MustCompile(reTrailing).Split(leading[len(leading)-1], 2)
+ return strings.TrimSpace(trailing[0])
+}
+
+func headersList(symbols []string, re string, src string) []string {
+ unformated := regexp.MustCompile(re).FindAllString(src, -1)
+ var formated = make([]string, len(unformated))
+ for index, value := range unformated {
+ formated[index] = removeLeadingAndTrailingSymbols(symbols, value)
+ }
+ return formated
+}
+
+func (mp *MarkdownParser) partOfTextWithSubheaders(prefix string, header string) string {
+ subprefixLen := strconv.Itoa(len(prefix) + 1)
+ re := strings.Join([]string{prefix, `[ ]*`, header, `[ ]*#?([^#]|#{`, subprefixLen, `,})+(`, prefix, `|\z)`}, "")
+ if prefix == "#" {
+ secondRE := strings.Join([]string{header, `[ ]*\n[=]+[^=]+([=]+|\z)`}, "")
+ re = strings.Join([]string{re, secondRE}, "|")
+ }
+ return regexp.MustCompile(re).FindString(mp.Text)
+}
+
+func (mp *MarkdownParser) Headers() []string {
+ return headersList([]string{`#`, `=`}, `(\n|\A)#[^#\n]+|(.)+\n[=]+`, mp.Text)
+}
+
+func (mp *MarkdownParser) SubHeadersOf(header string) []string {
+ partOfText := mp.partOfTextWithSubheaders("#", header)
+ return headersList([]string{`##`, `-`}, `(\n|\A)##[^#\n]+|(.)+\n[-]+`, partOfText)
+}
+
+func (mp *MarkdownParser) PhoneNumbers() []string {
+ re := regexp.MustCompile(`(\+|\()?\d+([ ()-]*\d+)+`)
+ return re.FindAllString(mp.Text, -1)
+}
+
+func (mp *MarkdownParser) Links() []string {
+ re := regexp.MustCompile(`\[[^\]]+\]\((?P<link>[^)]+)\)`)
+ matches := re.FindAllString(mp.Text, -1)
+ for index, value := range matches {
+ matches[index] = re.ReplaceAllString(value, `${link}`)
+ }
+ return matches
+}
+
+func (mp *MarkdownParser) Emails() []string {
+ re := regexp.MustCompile(`[a-zA-Z\d]+([-+._][a-zA-Z\d]+){0,200}@[a-zA-Z\d]+([-.][a-zA-Z\d]+)*\.[a-zA-Z]{2,}`)
+ return re.FindAllString(mp.Text, -1)
+}
+
+func (mp *MarkdownParser) GenerateTableOfContents() string {
+ return ""
+}
+
+func (mp *MarkdownParser) Names() []string {
+ return nil
+}
Headers() и SubHeathersOf() тотално не работят с README.md файла, а ако съдържанието на файла им се подаде като raw string работят...