Дойчин обнови решението на 03.12.2013 16:51 (преди над 4 години)
+package main
+
+import "regexp"
+import "strings"
+
+type MarkdownParser struct {
+ rawText string
+}
+
+func NewMarkdownParser(text string) (parser *MarkdownParser) {
+ parser = new(MarkdownParser)
+ parser.rawText = text
+ return
+}
+
+func (mp *MarkdownParser) Headers() (headers []string) {
+ re := regexp.MustCompile(`(?m)^(.+)\n=+$`)
+ for _, match := range re.FindAllStringSubmatch(mp.rawText, -1) {
+ headers = append(headers, match[1])
+ }
+
+ re = regexp.MustCompile(`(?m)^\s*#\s*(.+?)(\s*#+)?$`)
+ for _, match := range re.FindAllStringSubmatch(mp.rawText, -1) {
+ headers = append(headers, match[1])
+ }
+ return
+}
+
+func (mp *MarkdownParser) SubHeadersOf(header string) (subHeaders []string) {
+
+ return
+}
+
+func (mp *MarkdownParser) Names() (names []string) {
+ re := regexp.MustCompile(`(?s)[^\.!?;]\s+(([А-ЯA-Z][а-яa-z]+[\s-]*){2,})`)
+ for _, match := range re.FindAllStringSubmatch(mp.rawText, -1) {
+ names = append(names, strings.Trim(match[1], "\t\n\v\f\r -"))
+ }
+ return
+}
+
+func (mp *MarkdownParser) PhoneNumbers() (phoneNumbers []string) {
+ re := regexp.MustCompile(`(?s)(\+?\s*[\d\(\)-]+[\d \(\)-]+)`)
+ for _, match := range re.FindAllStringSubmatch(mp.rawText, -1) {
+ phoneNumbers = append(phoneNumbers, strings.Trim(match[1], "\t\n\v\f\r -"))
+ }
+ return
+}
+
+func (mp *MarkdownParser) Links() (links []string) {
+ links_re := `([a-zA-Z]+://[a-zA-Z][\w\.\-]+\.[\w\.\-]+[a-zA-Z](:\d+)?(/[/\w\?#_&%]+)?)`
+ re := regexp.MustCompile(links_re)
+ for _, match := range re.FindAllStringSubmatch(mp.rawText, -1) {
+ links = append(links, match[1])
+ }
+ return
+}
+
+func (mp *MarkdownParser) Emails() (emails []string) {
+ email_re := `([\w][\w\.\+]+@[a-zA-Z][\w\.\-]+\.[\w\.\-]+[a-zA-Z])`
+ re := regexp.MustCompile(email_re)
+ for _, match := range re.FindAllStringSubmatch(mp.rawText, -1) {
+ emails = append(emails, match[1])
+ }
+ return
+}
+
+func (mp *MarkdownParser) GenerateTableOfContents() string {
+ return ""
+}