diff --git a/pkg/parser/document_preprocessing.go b/pkg/parser/document_preprocessing.go index adba6f64..a90335ef 100644 --- a/pkg/parser/document_preprocessing.go +++ b/pkg/parser/document_preprocessing.go @@ -47,12 +47,10 @@ func preprocess(ctx *ParseContext, source io.Reader) (string, error) { switch e := element.(type) { case *types.AttributeDeclaration: ctx.attributes.set(e.Name, e.Value) - t, _ := e.RawText() - b.WriteString(t) + b.WriteString(e.RawText()) case *types.AttributeReset: ctx.attributes.unset(e.Name) - t, _ := e.RawText() - b.WriteString(t) + b.WriteString(e.RawText()) case *types.RawSection: b.WriteString(ctx.levelOffsets.apply(e)) case *types.FileInclusion: @@ -64,8 +62,7 @@ func preprocess(ctx *ParseContext, source io.Reader) (string, error) { case *types.BlockDelimiter: t.push(types.BlockDelimiterKind(e.Kind), e.Length) ctx.Opts = append(ctx.Opts, withinDelimitedBlock(t.withinDelimitedBlock())) - t, _ := e.RawText() - b.WriteString(t) + b.WriteString(e.RawText()) case types.ConditionalInclusion: if content, ok := e.SingleLineContent(); ok { if e.Eval(ctx.attributes.allAttributes()) { @@ -295,13 +292,6 @@ func absoluteOffset(offset int) *levelOffset { } } -func (l levelOffset) String() string { - if l.absolute { - return strconv.Itoa(l.value) - } - return fmt.Sprintf("+%d", l.value) -} - // lineRanges parses the `lines` attribute if it exists in the given FileInclusion, and returns // a corresponding `LineRanges` (or `false` if parsing failed to invalid input) func lineRanges(incl *types.FileInclusion) (types.LineRanges, bool, error) { @@ -361,12 +351,10 @@ func readWithinLines(scanner *bufio.Scanner, content *bytes.Buffer, lineRanges t } // TODO: stop reading if current line above highest range if lineRanges.Match(line) { - _, err := content.Write(scanner.Bytes()) - if err != nil { + if _, err := content.Write(scanner.Bytes()); err != nil { return err } - _, err = content.WriteString("\n") - if err != nil { + if _, err := content.WriteString("\n"); err != nil { return err } } diff --git a/pkg/parser/document_processing_apply_substitutions.go b/pkg/parser/document_processing_apply_substitutions.go index a2ceface..2e6a94d2 100644 --- a/pkg/parser/document_processing_apply_substitutions.go +++ b/pkg/parser/document_processing_apply_substitutions.go @@ -34,11 +34,6 @@ func applySubstitutionsOnFragment(ctx *ParseContext, f types.DocumentFragment) t log.Debugf("skipping substitutions because of fragment with error: %v", f.Error) return f } - // stats := &Stats{} - // opts := append(ctx.Opts, - // GlobalStore(types.AttrImagesDir, ctx.attributes.get(types.AttrImagesDir)), - // GlobalStore(usermacrosKey, ctx.userMacros), - // ) for i := range f.Elements { if err := applySubstitutionsOnElement(ctx, f.Elements[i], ctx.Opts...); err != nil { return types.NewErrorFragment(f.Position, err) @@ -491,7 +486,6 @@ func reparseAttributesInElements(elements []interface{}, subs []string, opts ... if err != nil { return err } - // e.SetAttributes(attrs) // needed? } } return nil @@ -542,8 +536,6 @@ func serialize(content []interface{}) ([]byte, *placeholders) { result := bytes.NewBuffer(nil) for _, element := range content { switch element := element.(type) { - case types.RawContent: - result.WriteString(string(element)) case types.RawLine: result.WriteString(string(element)) case *types.SinglelineComment: diff --git a/pkg/types/attributes.go b/pkg/types/attributes.go index 3aafcd7a..38c27531 100644 --- a/pkg/types/attributes.go +++ b/pkg/types/attributes.go @@ -539,9 +539,6 @@ func asString(v interface{}) (string, error) { switch v := v.(type) { case string: return v, nil - case RawText: - s, err := v.RawText() - return s, err case []interface{}: // complex attributes are wrapped in an []interface{} result := strings.Builder{} for _, value := range v { diff --git a/pkg/types/types.go b/pkg/types/types.go index 8272f7a8..e9f3b8b4 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -23,12 +23,6 @@ type Stringer interface { Stringify() string } -// RawText interface for the elements that can provide the rawText text representation of this element -// as it was (supposedly) written in the source document -type RawText interface { - RawText() (string, error) -} - // WithAttributes base interface for types on which attributes can be substituted type WithAttributes interface { GetAttributes() Attributes @@ -585,10 +579,8 @@ func NewAttributeDeclaration(name string, value interface{}, rawText string) (*A }, nil } -var _ RawText = &AttributeDeclaration{} - -func (a *AttributeDeclaration) RawText() (string, error) { - return a.rawText, nil +func (a *AttributeDeclaration) RawText() string { + return a.rawText } // AttributeReset the type for AttributeReset @@ -606,10 +598,8 @@ func NewAttributeReset(attrName string, rawText string) (*AttributeReset, error) }, nil } -var _ RawText = &AttributeReset{} - -func (a *AttributeReset) RawText() (string, error) { - return a.rawText, nil +func (a *AttributeReset) RawText() string { + return a.rawText } // AttributeReference the type for AttributeReference @@ -632,13 +622,6 @@ func NewAttributeSubstitution(name, rawText string) (interface{}, error) { nil } -var _ RawText = &AttributeReference{} - -// RawText returns the rawText text representation of this element as it was (supposedly) written in the source document -func (s *AttributeReference) RawText() (string, error) { - return s.rawText, nil -} - // PredefinedAttribute a special kind of attribute substitution, which // uses a predefined attribute type PredefinedAttribute AttributeReference @@ -665,12 +648,6 @@ func NewCounterSubstitution(name string, hidden bool, val interface{}, rawText s }, nil } -var _ RawText = &CounterSubstitution{} - -func (s *CounterSubstitution) RawText() (string, error) { - return s.rawText, nil -} - // ------------------------------------------ // Preamble // ------------------------------------------ @@ -761,41 +738,6 @@ type List struct { Elements []ListElement } -var _ WithElements = &List{} - -func (l *List) GetAttributes() Attributes { - return l.Attributes -} - -func (l *List) AddAttributes(attrs Attributes) { - l.Attributes.AddAll(attrs) -} - -func (l *List) SetAttributes(attrs Attributes) { - l.Attributes.SetAll(attrs) -} - -func (l *List) GetElements() []interface{} { - elements := make([]interface{}, len(l.Elements)) - for i, e := range l.Elements { - elements[i] = e - } - return elements -} - -func (l *List) SetElements(elements []interface{}) error { - elmts := make([]ListElement, len(elements)) - for i, e := range elements { - if e, ok := e.(ListElement); ok { - elmts[i] = e - continue - } - return fmt.Errorf("unexpected type of list element: '%T'", e) - } - l.Elements = elmts - return nil -} - // CanAddElement checks if the given element can be added func (l *List) CanAddElement(element interface{}) bool { switch e := element.(type) { @@ -2212,10 +2154,8 @@ func NewMarkdownCodeBlockDelimiter(language, rawText string) (*BlockDelimiter, e }, nil } -var _ RawText = &BlockDelimiter{} - -func (b *BlockDelimiter) RawText() (string, error) { - return b.rawText, nil +func (b *BlockDelimiter) RawText() string { + return b.rawText } // DelimitedBlock the structure for the Listing blocks @@ -2696,37 +2636,6 @@ func NewQuotedText(kind QuotedTextKind, elements ...interface{}) (*QuotedText, e }, nil } -var _ RawText = &QuotedText{} - -// RawText returns the rawText text representation of this element as it was (supposedly) written in the source document -func (t *QuotedText) RawText() (string, error) { - result := strings.Builder{} - result.WriteString(string(t.Kind)) // opening delimiter - s, err := toRawText(t.Elements) - if err != nil { - return "", err - } - result.WriteString(s) - result.WriteString(string(t.Kind)) // closing delimiter - return result.String(), nil -} - -func toRawText(elements []interface{}) (string, error) { - result := strings.Builder{} - for _, e := range elements { - r, ok := e.(RawText) - if !ok { - return "", fmt.Errorf("element of type '%T' cannot be converted to string", e) - } - s, err := r.RawText() - if err != nil { - return "", err - } - result.WriteString(s) - } - return result.String(), nil -} - var _ WithElements = &QuotedText{} // GetElements returns this QuotedText's elements @@ -2822,31 +2731,6 @@ func NewInlinePassthrough(kind PassthroughKind, elements []interface{}) (*Inline }, nil } -var _ RawText = &InlinePassthrough{} - -// RawText returns the rawText text representation of this element as it was (supposedly) written in the source document -func (p *InlinePassthrough) RawText() (string, error) { - result := strings.Builder{} - switch p.Kind { - case PassthroughMacro: - result.WriteString("pass:[") // opening delimiter - default: - result.WriteString(string(p.Kind)) // opening delimiter - } - e, err := toRawText(p.Elements) - if err != nil { - return "", err - } - result.WriteString(e) - switch p.Kind { - case PassthroughMacro: - result.WriteString("]") // closing delimiter - default: - result.WriteString(string(p.Kind)) // closing delimiter - } - return result.String(), nil -} - // ------------------------------------------ // Inline Links // ------------------------------------------ @@ -3253,17 +3137,6 @@ func (l RawLine) trim() RawLine { return RawLine(strings.TrimSpace(string(l))) } -// ------------------------------------------------------------------------------------- -// Raw Content -// ------------------------------------------------------------------------------------- -type RawContent string - -// NewRawContent returns a new RawContent wrapper for the given string -func NewRawContent(content string) (RawContent, error) { - // log.Debugf("new line: '%v'", content) - return RawContent(content), nil -} - // ------------------------------------------------------------------------------------- // LineRanges: one or more ranges of lines to limit the content of a file to include // ------------------------------------------------------------------------------------- @@ -3611,13 +3484,6 @@ func NewSpecialCharacter(name string) (*SpecialCharacter, error) { }, nil } -var _ RawText = SpecialCharacter{} - -// RawText returns the rawText text representation of this element as it was (supposedly) written in the source document -func (c SpecialCharacter) RawText() (string, error) { - return c.Name, nil -} - // Symbol a sequence of characters, which may get a special treatment later during rendering // Eg: `(C)`, `(TM)`, `...`, etc. type Symbol struct { @@ -3640,12 +3506,6 @@ func NewSymbolWithForeword(name, foreword string) (*Symbol, error) { }, nil } -var _ RawText = &Symbol{} - -func (s *Symbol) RawText() (string, error) { - return s.Name, nil -} - // ------------------------------------------------------------------------------------ // ElementPlaceHolder // They need to be identified as they may have a special treatment during the rendering @@ -4014,13 +3874,6 @@ func NewTableRow(elements []interface{}) (*TableRow, error) { }, nil } -func (r *TableRow) AddCell(c *TableCell) { - if r.Cells == nil { - r.Cells = []*TableCell{} - } - r.Cells = append(r.Cells, c) -} - var _ WithElements = &TableRow{} func (r *TableRow) GetAttributes() Attributes { diff --git a/pkg/types/types_test.go b/pkg/types/types_test.go index aaf1e5b1..60f6e656 100644 --- a/pkg/types/types_test.go +++ b/pkg/types/types_test.go @@ -980,179 +980,6 @@ var _ = DescribeTable("no match attribute with key", Entry("no match for block-kind: quote", types.AttrID, true), ) -var _ = DescribeTable("rawtext", - func(element types.RawText, expected string) { - Expect(element.RawText()).To(Equal(expected)) - }, - // quoted text - Entry("single quote bold text", - &types.QuotedText{ - Kind: types.SingleQuoteBold, - Elements: []interface{}{ - &types.StringElement{ - Content: "content", - }, - }, - }, - "*content*"), - Entry("double quote bold text", - &types.QuotedText{ - Kind: types.DoubleQuoteBold, - Elements: []interface{}{ - &types.StringElement{ - Content: "content", - }, - }, - }, - "**content**"), - Entry("single quote italic text", - &types.QuotedText{ - Kind: types.SingleQuoteItalic, - Elements: []interface{}{ - &types.StringElement{ - Content: "content", - }, - }, - }, - "_content_"), - Entry("double quote italic text", - &types.QuotedText{ - Kind: types.DoubleQuoteItalic, - Elements: []interface{}{ - &types.StringElement{ - Content: "content", - }, - }, - }, - "__content__"), - Entry("single quote monospace text", - &types.QuotedText{ - Kind: types.SingleQuoteMonospace, - Elements: []interface{}{ - &types.StringElement{ - Content: "content", - }, - }, - }, - "`content`"), - Entry("double quote monospace text", - &types.QuotedText{ - Kind: types.DoubleQuoteMonospace, - Elements: []interface{}{ - &types.StringElement{ - Content: "content", - }, - }, - }, - "``content``"), - Entry("single quote marked text", - &types.QuotedText{ - Kind: types.SingleQuoteMarked, - Elements: []interface{}{ - &types.StringElement{ - Content: "content", - }, - }, - }, - "#content#"), - Entry("double quote marked text", - &types.QuotedText{ - Kind: types.DoubleQuoteMarked, - Elements: []interface{}{ - &types.StringElement{ - Content: "content", - }, - }, - }, - "##content##"), - Entry("single quote subscript text", - &types.QuotedText{ - Kind: types.SingleQuoteSubscript, - Elements: []interface{}{ - &types.StringElement{ - Content: "content", - }, - }, - }, - "~content~"), - Entry("single quote superscript text", - &types.QuotedText{ - Kind: types.SingleQuoteSuperscript, - Elements: []interface{}{ - &types.StringElement{ - Content: "content", - }, - }, - }, - "^content^"), - // inline passthrough - Entry("singleplus inline passthrough", - &types.InlinePassthrough{ - Kind: types.SinglePlusPassthrough, - Elements: []interface{}{ - &types.StringElement{ - Content: "content", - }, - }, - }, - "+content+"), - Entry("tripleplus inline passthrough", - &types.InlinePassthrough{ - Kind: types.TriplePlusPassthrough, - Elements: []interface{}{ - &types.StringElement{ - Content: "content", - }, - }, - }, - "+++content+++"), - Entry("macro inline passthrough", - &types.InlinePassthrough{ - Kind: types.PassthroughMacro, - Elements: []interface{}{ - &types.StringElement{ - Content: "content", - }, - }, - }, - "pass:[content]"), - // special characters - Entry("special character", - &types.SpecialCharacter{ - Name: "<", - }, - "<"), - // mixins - Entry("mixins", - &types.QuotedText{ - Kind: types.SingleQuoteBold, - Elements: []interface{}{ - &types.StringElement{ - Content: "some ", - }, - &types.Symbol{ - Name: "\"`", - }, - &types.StringElement{ - Content: "content", - }, - &types.SpecialCharacter{ - Name: "<", - }, - &types.SpecialCharacter{ - Name: ">", - }, - &types.Symbol{ - Name: "`\"", - }, - &types.StringElement{ - Content: " ", - }, - }, - }, - "*some \"`content<>`\" *"), -) - var _ = DescribeTable("ifeval operands", func(operand types.IfevalOperand, left, right interface{}, expected bool) {