Skip to content

Commit

Permalink
refactor(types): remove unused code (#1022)
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored May 16, 2022
1 parent 6f86cf4 commit efa738e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 354 deletions.
22 changes: 5 additions & 17 deletions pkg/parser/document_preprocessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
}
Expand Down
8 changes: 0 additions & 8 deletions pkg/parser/document_processing_apply_substitutions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -491,7 +486,6 @@ func reparseAttributesInElements(elements []interface{}, subs []string, opts ...
if err != nil {
return err
}
// e.SetAttributes(attrs) // needed?
}
}
return nil
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions pkg/types/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
159 changes: 6 additions & 153 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
// ------------------------------------------
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
// ------------------------------------------
Expand Down Expand Up @@ -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
// -------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit efa738e

Please sign in to comment.