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

refactor(parser): simplify DocumentFragment rule #1038

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
37 changes: 35 additions & 2 deletions pkg/parser/document_preprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func preprocess(ctx *ParseContext, source io.Reader) (string, error) {
}
b.WriteString(f)
case *types.BlockDelimiter:
t.push(e.Kind, e.Length)
ctx.opts = append(ctx.opts, withinDelimitedBlock(t.withinDelimitedBlock()))
t.track(e.Kind, e.Length)
ctx.opts = append(ctx.opts, t.withinDelimitedBlock())
b.WriteString(e.RawText())
case types.ConditionalInclusion:
if content, ok := e.SingleLineContent(); ok {
Expand All @@ -81,6 +81,39 @@ func preprocess(ctx *ParseContext, source io.Reader) (string, error) {
return b.String(), nil
}

type blockDelimiterTracker struct {
stack []blockDelimiter
}

type blockDelimiter struct {
kind string
length int
}

func newBlockDelimiterTracker() *blockDelimiterTracker {
return &blockDelimiterTracker{
stack: []blockDelimiter{},
}
}

func (t *blockDelimiterTracker) track(kind string, length int) {
switch {
case len(t.stack) > 0 && t.stack[len(t.stack)-1].kind == kind && t.stack[len(t.stack)-1].length == length:
// pop
t.stack = t.stack[:len(t.stack)-1]
default:
// push
t.stack = append(t.stack, blockDelimiter{
kind: kind,
length: length,
})
}
}

func (t *blockDelimiterTracker) withinDelimitedBlock() Option {
return GlobalStore(withinDelimitedBlockKey, len(t.stack) > 0)
}

// replace the content of this FileInclusion element the content of the target file
// note: there is a trade-off here: we include the whole content of the file in the current
// fragment, making it potentially big, but at the same time we ensure that the context
Expand Down
4 changes: 2 additions & 2 deletions pkg/parser/document_processing_parse_fragments.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func reparseDelimitedBlock(ctx *ParseContext, b *types.DelimitedBlock) error {
switch b.Kind {
case types.Example, types.Quote, types.Sidebar, types.Open:
log.Debugf("parsing elements of delimited block of kind '%s'", b.Kind)
opts := append(ctx.opts, Entrypoint("DelimitedBlockElements"), withinDelimitedBlock(true))
opts := append(ctx.opts, Entrypoint("DelimitedBlockElements"))
elements, err := reparseElements(b.Elements, opts...)
if err != nil {
return err
Expand Down Expand Up @@ -238,6 +238,6 @@ func (c *current) isWithinLiteralParagraph() bool {
log.Debugf("within literal paragraph: %t", attrs[types.AttrStyle] == types.Literal)
return attrs[types.AttrPositional1] == types.Literal || attrs[types.AttrStyle] == types.Literal
}
log.Debug("not within literal paragraph")
// log.Debug("not within literal paragraph")
return false
}
Loading