Skip to content

Commit

Permalink
use regexputil
Browse files Browse the repository at this point in the history
  • Loading branch information
seletskiy committed Aug 8, 2019
1 parent 559b913 commit 6b83d14
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func main() {
log.Fatal(err)
}

meta, err := mark.ExtractMeta(markdown)
meta, markdown, err := mark.ExtractMeta(markdown)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -190,7 +190,7 @@ func main() {
}
}

macros, markdown, err := macro.LoadMacros(markdown, templates)
macros, markdown, err := macro.ExtractMacros(markdown, templates)
if err != nil {
log.Fatal(err)
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/mark/includes/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import (

var (
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>.*?))?-->`,
)
)

Expand Down
28 changes: 19 additions & 9 deletions pkg/mark/macro/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ import (
"github.com/kovetskiy/mark/pkg/log"
"github.com/kovetskiy/mark/pkg/mark/includes"
"github.com/reconquest/karma-go"
"github.com/reconquest/regexputil-go"
"gopkg.in/yaml.v2"
)

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 {
Expand Down Expand Up @@ -88,7 +96,7 @@ func (macro *Macro) configure(node interface{}, groups [][]byte) interface{} {
return node
}

func LoadMacros(
func ExtractMacros(
contents []byte,
templates *template.Template,
) ([]Macro, []byte, error) {
Expand All @@ -103,15 +111,17 @@ func LoadMacros(
return spec
}

groups := reMacroDirective.FindSubmatch(spec)
groups := reMacroDirective.FindStringSubmatch(string(spec))

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.Template, err = includes.LoadTemplate(path, templates)
macro.Template, err = includes.LoadTemplate(template, templates)

if err != nil {
err = karma.Format(err, "unable to load template")
Expand All @@ -120,10 +130,10 @@ func LoadMacros(
}

facts := karma.
Describe("template", path).
Describe("expr", string(expr))
Describe("template", template).
Describe("expr", expr)

macro.Regexp, err = regexp.Compile(string(expr))
macro.Regexp, err = regexp.Compile(expr)
if err != nil {
err = facts.
Format(
Expand All @@ -134,7 +144,7 @@ func LoadMacros(
return nil
}

err = yaml.Unmarshal(config, &macro.Config)
err = yaml.Unmarshal([]byte(config), &macro.Config)
if err != nil {
err = facts.
Describe("config", string(config)).
Expand Down
29 changes: 17 additions & 12 deletions pkg/mark/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,30 @@ type Meta struct {
Attachments []string
}

func ExtractMeta(data []byte) (*Meta, error) {
var (
reHeaderPatternV1 = regexp.MustCompile(`\[\]:\s*#\s*\(([^:]+):\s*(.*)\)`)
reHeaderPatternV2 = regexp.MustCompile(`<!--\s*([^:]+):\s*(.*)\s*-->`)
)

func ExtractMeta(data []byte) (*Meta, []byte, error) {
var (
headerPatternV1 = regexp.MustCompile(`\[\]:\s*#\s*\(([^:]+):\s*(.*)\)`)
headerPatternV2 = regexp.MustCompile(`<!--\s*([^:]+):\s*(.*)\s*-->`)
meta *Meta
offset int
)

var meta *Meta

scanner := bufio.NewScanner(bytes.NewBuffer(data))
for scanner.Scan() {
line := scanner.Text()

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 {
matches = headerPatternV1.FindStringSubmatch(line)
matches = reHeaderPatternV1.FindStringSubmatch(line)
if matches == nil {
break
}
Expand Down Expand Up @@ -97,22 +102,22 @@ func ExtractMeta(data []byte) (*Meta, error) {
}

if meta == nil {
return nil, nil
return nil, data, nil
}

if meta.Space == "" {
return nil, fmt.Errorf(
return nil, nil, fmt.Errorf(
"space key is not set (%s header is not set)",
HeaderSpace,
)
}

if meta.Title == "" {
return nil, fmt.Errorf(
return nil, nil, fmt.Errorf(
"page title is not set (%s header is not set)",
HeaderTitle,
)
}

return meta, nil
return nil, data[offset+1:], nil
}
2 changes: 1 addition & 1 deletion pkg/mark/stdlib/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func macros(templates *template.Template) ([]macro.Macro, error) {
return []byte(strings.Join(line, "\n"))
}

macros, _, err := macro.LoadMacros(
macros, _, err := macro.ExtractMacros(
[]byte(text(
`<!-- Macro: @\{([^}]+)\}`,
` Template: ac:link:user`,
Expand Down

0 comments on commit 6b83d14

Please sign in to comment.