You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.6 KiB
61 lines
1.6 KiB
// package markdown includes basic markdown parsing for reTangleD. |
|
// The interface is subject to change at any time |
|
// It is mostly an implementation of the [attributes CommonMark proposal](https://talk.commonmark.org/t/consistent-attribute-syntax/272) at the moment |
|
package markdown |
|
|
|
import ( |
|
"strings" |
|
) |
|
|
|
// Attributes contains parsed Markdown attributes from the form: |
|
// `{#id .class file=main.c other="test okay"}` |
|
type Attributes struct { |
|
ID string |
|
Classes []string |
|
// Other includes attributes in the format file=main.c |
|
Other map[string]string |
|
} |
|
|
|
// GetAttributes finds the attributes from a Markdown line |
|
// Returns a blank Attributes struct if there are no attributes |
|
func GetAttributes(line string) (Attributes) { |
|
attrs := "" |
|
if strings.Index(line, "{") != -1 { |
|
attrs = line[strings.Index(line, "{"):] |
|
} |
|
|
|
// If it only contains `{}` and nothing more, or not even that |
|
if len(attrs) < 2 { |
|
return Attributes{} |
|
} |
|
|
|
if attrs[0] != '{' || attrs[len(attrs)-1] != '}' { |
|
return Attributes{} |
|
} |
|
|
|
// proceed with all the attrs without { and } |
|
return attribute(attrs[1:len(attrs)-1]) |
|
} |
|
|
|
// Attribute parses attributes into an Attributes struct |
|
// TODO: handle "this format" (so don't split by spaces) |
|
func attribute(text string) (Attributes) { |
|
attrs := strings.Split(text, " ") |
|
|
|
result := Attributes{} |
|
result.Other = map[string]string{} |
|
for _, attr := range attrs { |
|
switch attr[0] { |
|
case '#': |
|
result.ID = attr[1:] |
|
case '.': |
|
result.Classes = append(result.Classes, attr[1:]) |
|
default: |
|
if strings.Index(attr, "=") > 0 { |
|
result.Other[strings.Split(attr, "=")[0]] = strings.Split(attr, "=")[1] |
|
} |
|
} |
|
} |
|
|
|
return result |
|
}
|
|
|