Skip to content

Commit

Permalink
feat(parser): support wildcards in tag inclusions
Browse files Browse the repository at this point in the history
Support '*' and '**' wildcards and negated ranges, too.

Fixes bytesparadise#396

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon committed Oct 20, 2019
1 parent 37e48f8 commit 8641209
Show file tree
Hide file tree
Showing 10 changed files with 3,226 additions and 2,293 deletions.
27 changes: 17 additions & 10 deletions pkg/parser/file_inclusion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"text/template"

"github.com/bytesparadise/libasciidoc/pkg/types"
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"

log "github.com/sirupsen/logrus"
Expand All @@ -26,6 +27,7 @@ func init() {
func parseFileToInclude(filename string, incl types.FileInclusion, attrs types.DocumentAttributes, opts ...Option) (types.PreflightDocument, error) {
path := incl.Location.Resolve(attrs)
log.Debugf("parsing '%s'...", path)
log.Debugf("file inclusion attributes: %s", spew.Sdump(incl.Attributes))
f, absPath, done, err := open(path)
defer done()
if err != nil {
Expand Down Expand Up @@ -123,7 +125,7 @@ func readWithinLines(scanner *bufio.Scanner, content *bytes.Buffer, lineRanges t

func readWithinTags(path string, scanner *bufio.Scanner, content *bytes.Buffer, expectedRanges types.TagRanges) error {
log.Debugf("limiting to tag ranges: %v", expectedRanges)
actualRanges := make(map[string]*types.TagRange, len(expectedRanges)) // ensure capacity
currentRanges := make(map[string]*types.CurrentTagRange, len(expectedRanges)) // ensure capacity
lineNumber := 0
for scanner.Scan() {
lineNumber++
Expand All @@ -139,15 +141,15 @@ func readWithinTags(path string, scanner *bufio.Scanner, content *bytes.Buffer,
}
// check if a start or end tag was found in the line
if startTag, ok := fl.GetStartTag(); ok {
actualRanges[startTag.Value] = &types.TagRange{
currentRanges[startTag.Value] = &types.CurrentTagRange{
StartLine: lineNumber,
EndLine: -1,
}
}
if endTag, ok := fl.GetEndTag(); ok {
actualRanges[endTag.Value].EndLine = lineNumber
currentRanges[endTag.Value].EndLine = lineNumber
}
if expectedRanges.Match(lineNumber, actualRanges) && !fl.HasTag() {
if expectedRanges.Match(lineNumber, currentRanges) && !fl.HasTag() {
_, err := content.Write(scanner.Bytes())
if err != nil {
return err
Expand All @@ -160,12 +162,17 @@ func readWithinTags(path string, scanner *bufio.Scanner, content *bytes.Buffer,
}
// after the file has been processed, let's check if all tags were "found"
for _, tag := range expectedRanges {
log.Debugf("checking if tag '%s' was found...", tag)
tr, found := actualRanges[tag]
if !found {
log.Errorf("tag '%s' not found in include file: %s", tag, path)
} else if tr.EndLine == -1 {
log.Errorf("detected unclosed tag '%s' starting at line %d of include file: %s", tag, tr.StartLine, path)
log.Debugf("checking if tag '%s' was found...", tag.Name)
switch tag.Name {
case "*", "**":
continue
default:
tr, found := currentRanges[tag.Name]
if !found {
log.Errorf("tag '%s' not found in include file: %s", tag.Name, path)
} else if tr.EndLine == -1 {
log.Errorf("detected unclosed tag '%s' starting at line %d of include file: %s", tag.Name, tr.StartLine, path)
}
}
}
return nil
Expand Down
Loading

0 comments on commit 8641209

Please sign in to comment.