Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser): support attribute in path of file to include #355

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions pkg/parser/asciidoc-grammar.peg
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,8 @@ TableOfContentsMacro <- "toc::[]" NEWLINE
// ------------------------------------------
// File inclusions
// ------------------------------------------
FileInclusion <- incl:("include::" path:(URL) inlineAttributes:(FileIncludeAttributes) {
return types.NewFileInclusion(path.(string), inlineAttributes.(types.ElementAttributes), string(c.text))
FileInclusion <- incl:("include::" path:(Location) inlineAttributes:(FileIncludeAttributes) {
return types.NewFileInclusion(path.(types.Location), inlineAttributes.(types.ElementAttributes), string(c.text))
}) WS* EOL {
return incl.(types.FileInclusion), nil
}
Expand Down Expand Up @@ -1514,15 +1514,19 @@ Dot <- "." {
}

Word <- (Alphanums / QuotedTextPrefix / ((!NEWLINE !WS !Parenthesis !"." !QuotedTextPrefix .){
return string(c.text), nil
return types.NewStringElement(string(c.text))
})+ / "."+){ // word cannot contain parenthesis. Dots and ellipsis are treated as independent words (but will be combined afterwards)
return string(c.text), nil
return types.NewStringElement(string(c.text))
}

Spaces <- WS+ {
return string(c.text), nil
}

Location <- elements:(URL_SCHEME? (DocumentAttributeSubstitution / Word)+) {
return types.NewLocation(elements.([]interface{}))
}

URL <- (Alphanums / (!NEWLINE !WS !"[" !"]" .){
return string(c.text), nil
})+ {
Expand All @@ -1542,7 +1546,6 @@ URL_TEXT <- (Alphanums / (!NEWLINE !"[" !"]" .){
}

URL_SCHEME <- "http://" / "https://" / "ftp://" / "irc://" / "mailto:"

DIGIT <- [0-9] {
return string(c.text), nil
}
Expand Down
48,468 changes: 25,631 additions & 22,837 deletions pkg/parser/asciidoc_parser.go

Large diffs are not rendered by default.

17 changes: 10 additions & 7 deletions pkg/parser/document_preprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ func preparseDocument(filename string, r io.Reader, levelOffset string, opts ...
}
doc := d.(types.PreparsedDocument)
result := bytes.NewBuffer(nil)
attrs := map[string]string{}
for i, e := range doc.Elements {
switch e := e.(type) {
case types.FileInclusion:
// read the file and include its content
content, err := parseFileToInclude(e, opts...)
content, err := parseFileToInclude(e, attrs, opts...)
if err != nil {
// do not fail, but instead report the error in the console
log.Errorf("failed to include file '%s': %v", e.Path, err)
log.Errorf("failed to include file '%s': %v", e.Location, err)
}
result.Write(content)
case types.DocumentAttributeDeclaration:
attrs[e.Name] = e.Value
result.WriteRune(':')
result.WriteString(e.Name)
result.WriteRune(':')
Expand Down Expand Up @@ -90,16 +92,17 @@ func invalidFileErrMsg(incl types.FileInclusion) []byte {
return buf.Bytes()
}

func parseFileToInclude(incl types.FileInclusion, opts ...Option) ([]byte, error) {
log.Debugf("parsing '%s'...", incl.Path)
func parseFileToInclude(incl types.FileInclusion, attrs map[string]string, opts ...Option) ([]byte, error) {
path := incl.Location.Resolve(attrs)
log.Debugf("parsing '%s'...", path)
// manage new working directory based on the file's location
// so that if this file also includes other files with relative path,
// then the it can work ;)
wd, err := os.Getwd()
if err != nil {
return nil, err
}
absPath, err := filepath.Abs(incl.Path)
absPath, err := filepath.Abs(path)
if err != nil {
return invalidFileErrMsg(incl), err
}
Expand Down Expand Up @@ -157,7 +160,7 @@ func parseFileToInclude(incl types.FileInclusion, opts ...Option) ([]byte, error
return invalidFileErrMsg(incl), errors.Wrap(err, "unable to read file to include")
}
if levelOffset, ok := incl.Attributes[types.AttrLevelOffset].(string); ok {
return preparseDocument(incl.Path, content, levelOffset, opts...)
return preparseDocument(path, content, levelOffset, opts...)
}
return preparseDocument(incl.Path, content, "", opts...)
return preparseDocument(path, content, "", opts...)
}
Loading