use regexputil

This commit is contained in:
Stanislav Seletskiy 2019-08-08 23:41:26 +03:00
parent 559b913900
commit 6b83d140d5
No known key found for this signature in database
GPG Key ID: E6B40F71C367E6B5
5 changed files with 46 additions and 26 deletions

View File

@ -162,7 +162,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
meta, err := mark.ExtractMeta(markdown) meta, markdown, err := mark.ExtractMeta(markdown)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -190,7 +190,7 @@ func main() {
} }
} }
macros, markdown, err := macro.LoadMacros(markdown, templates) macros, markdown, err := macro.ExtractMacros(markdown, templates)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -17,7 +17,12 @@ import (
var ( var (
reIncludeDirective = regexp.MustCompile( reIncludeDirective = regexp.MustCompile(
`(?s)<!--\s*Include:\s*(\S+)(.*?)-->`, // <!-- Include: <template path>
// <optional yaml data> -->
`(?s)` + // dot capture newlines
/**/ `<!--\s*Include:\s*(?P<template>\S+)\s*` +
/* */ `(\n(?P<config>.*?))?-->`,
) )
) )

View File

@ -10,11 +10,19 @@ import (
"github.com/kovetskiy/mark/pkg/log" "github.com/kovetskiy/mark/pkg/log"
"github.com/kovetskiy/mark/pkg/mark/includes" "github.com/kovetskiy/mark/pkg/mark/includes"
"github.com/reconquest/karma-go" "github.com/reconquest/karma-go"
"github.com/reconquest/regexputil-go"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
var reMacroDirective = regexp.MustCompile( var reMacroDirective = regexp.MustCompile(
`(?s)<!--\s*Macro:\s*([^\n]+)\n\s*Template:\s*(\S+)(.*?)-->`, // <!-- Macro: <regexp>
// Template: <template path>
// <optional yaml data> -->
`(?s)` + // dot capture newlines
/**/ `<!--\s*Macro:\s*(?P<expr>[^\n]+)\n` +
/* */ `\s*Template:\s*(?P<template>\S+)\s*` +
/* */ `(\n(?P<config>.*?))?-->`,
) )
type Macro struct { type Macro struct {
@ -88,7 +96,7 @@ func (macro *Macro) configure(node interface{}, groups [][]byte) interface{} {
return node return node
} }
func LoadMacros( func ExtractMacros(
contents []byte, contents []byte,
templates *template.Template, templates *template.Template,
) ([]Macro, []byte, error) { ) ([]Macro, []byte, error) {
@ -103,15 +111,17 @@ func LoadMacros(
return spec return spec
} }
groups := reMacroDirective.FindSubmatch(spec) groups := reMacroDirective.FindStringSubmatch(string(spec))
var ( var (
expr, path, config = groups[1], string(groups[2]), groups[3] expr = regexputil.Subexp(reMacroDirective, groups, "expr")
template = regexputil.Subexp(reMacroDirective, groups, "template")
config = regexputil.Subexp(reMacroDirective, groups, "config")
macro Macro macro Macro
) )
macro.Template, err = includes.LoadTemplate(path, templates) macro.Template, err = includes.LoadTemplate(template, templates)
if err != nil { if err != nil {
err = karma.Format(err, "unable to load template") err = karma.Format(err, "unable to load template")
@ -120,10 +130,10 @@ func LoadMacros(
} }
facts := karma. facts := karma.
Describe("template", path). Describe("template", template).
Describe("expr", string(expr)) Describe("expr", expr)
macro.Regexp, err = regexp.Compile(string(expr)) macro.Regexp, err = regexp.Compile(expr)
if err != nil { if err != nil {
err = facts. err = facts.
Format( Format(
@ -134,7 +144,7 @@ func LoadMacros(
return nil return nil
} }
err = yaml.Unmarshal(config, &macro.Config) err = yaml.Unmarshal([]byte(config), &macro.Config)
if err != nil { if err != nil {
err = facts. err = facts.
Describe("config", string(config)). Describe("config", string(config)).

View File

@ -26,25 +26,30 @@ type Meta struct {
Attachments []string Attachments []string
} }
func ExtractMeta(data []byte) (*Meta, error) { var (
var ( reHeaderPatternV1 = regexp.MustCompile(`\[\]:\s*#\s*\(([^:]+):\s*(.*)\)`)
headerPatternV1 = regexp.MustCompile(`\[\]:\s*#\s*\(([^:]+):\s*(.*)\)`) reHeaderPatternV2 = regexp.MustCompile(`<!--\s*([^:]+):\s*(.*)\s*-->`)
headerPatternV2 = regexp.MustCompile(`<!--\s*([^:]+):\s*(.*)\s*-->`) )
)
var meta *Meta func ExtractMeta(data []byte) (*Meta, []byte, error) {
var (
meta *Meta
offset int
)
scanner := bufio.NewScanner(bytes.NewBuffer(data)) scanner := bufio.NewScanner(bytes.NewBuffer(data))
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
return nil, err return nil, nil, err
} }
matches := headerPatternV2.FindStringSubmatch(line) offset += len(line)
matches := reHeaderPatternV2.FindStringSubmatch(line)
if matches == nil { if matches == nil {
matches = headerPatternV1.FindStringSubmatch(line) matches = reHeaderPatternV1.FindStringSubmatch(line)
if matches == nil { if matches == nil {
break break
} }
@ -97,22 +102,22 @@ func ExtractMeta(data []byte) (*Meta, error) {
} }
if meta == nil { if meta == nil {
return nil, nil return nil, data, nil
} }
if meta.Space == "" { if meta.Space == "" {
return nil, fmt.Errorf( return nil, nil, fmt.Errorf(
"space key is not set (%s header is not set)", "space key is not set (%s header is not set)",
HeaderSpace, HeaderSpace,
) )
} }
if meta.Title == "" { if meta.Title == "" {
return nil, fmt.Errorf( return nil, nil, fmt.Errorf(
"page title is not set (%s header is not set)", "page title is not set (%s header is not set)",
HeaderTitle, HeaderTitle,
) )
} }
return meta, nil return nil, data[offset+1:], nil
} }

View File

@ -40,7 +40,7 @@ func macros(templates *template.Template) ([]macro.Macro, error) {
return []byte(strings.Join(line, "\n")) return []byte(strings.Join(line, "\n"))
} }
macros, _, err := macro.LoadMacros( macros, _, err := macro.ExtractMacros(
[]byte(text( []byte(text(
`<!-- Macro: @\{([^}]+)\}`, `<!-- Macro: @\{([^}]+)\}`,
` Template: ac:link:user`, ` Template: ac:link:user`,