foxmarks

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2021 License: MIT Imports: 9 Imported by: 0

README

foxmarks

A CommonMark parser and render written in Go

Implementation Progress

Incomplete. See implementation.md for more information.

Usage

import "foxmarks"

f := Open("path_to_file")
r := f.Parse().Render()

// do something with r

Copyright 2021 Flipp Syder under the MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BlockHTMLTags map[BlockObjectType]BlockHTMLRender = map[BlockObjectType]BlockHTMLRender{
	Paragraph: BlockHTMLRender{
		Opener: "<p>",
		Closer: "</p>",
	},
	List: BlockHTMLRender{
		Opener:       "<ul>",
		Closer:       "</ul>",
		CleanContent: true,
	},
	ListItem: BlockHTMLRender{
		Opener: "<li>",
		Closer: "</li>",
	},
	ThematicBreak: BlockHTMLRender{
		Opener:       "",
		Closer:       "<hr />",
		CleanContent: true,
	},
	Header1: BlockHTMLRender{
		Opener: "<h1>",
		Closer: "</h1>",
	},
	Header2: BlockHTMLRender{
		Opener: "<h2>",
		Closer: "</h2>",
	},
	Header3: BlockHTMLRender{
		Opener: "<h3>",
		Closer: "</h3>",
	},
	Header4: BlockHTMLRender{
		Opener: "<h4>",
		Closer: "</h4>",
	},
	Header5: BlockHTMLRender{
		Opener: "<h5>",
		Closer: "</h5>",
	},
	Header6: BlockHTMLRender{
		Opener: "<h6>",
		Closer: "</h6>",
	},
	CodeBlock: BlockHTMLRender{
		Opener: "<pre><code>",
		Closer: "</code></pre>",
	},
}
View Source
var Debug bool
View Source
var InlineHTMLTags map[InlineObjectType]InlineHTMLTagset = map[InlineObjectType]InlineHTMLTagset{
	StrongText: InlineHTMLTagset{
		Opener: func(i *InlineTag) string { return "<strong>" },
		Closer: func(i *InlineTag) string { return "</strong>" },
		OpLen:  func(i *InlineTag) int { return 2 },
	},
	EmText: InlineHTMLTagset{
		Opener: func(i *InlineTag) string { return "<em>" },
		Closer: func(i *InlineTag) string { return "</em>" },
		OpLen:  func(i *InlineTag) int { return 1 },
	},
	CodeSpan: InlineHTMLTagset{
		Opener: func(i *InlineTag) string { return "<code>" },
		Closer: func(i *InlineTag) string { return "</code>" },
		OpLen:  func(i *InlineTag) int { return 1 },
	},
	LinkRef: InlineHTMLTagset{
		Opener: func(i *InlineTag) string { return "<a href=\"" + i.Content[1] + "\">" },
		Closer: func(i *InlineTag) string { return "</a>" },
		OpLen:  func(i *InlineTag) int { return 1 },
	},
	Link: InlineHTMLTagset{
		Opener: func(i *InlineTag) string { return "" },
		Closer: func(i *InlineTag) string { return "" },
		OpLen: func(i *InlineTag) int {
			if i.Tag == Opener {
				return len(i.Content[0]) + 2
			}

			return 0
		},
	},
	ImageRef: InlineHTMLTagset{
		Opener: func(i *InlineTag) string { return "<img src=\"" + i.Content[1] + "\">" },
		Closer: func(i *InlineTag) string { return "" },
		OpLen: func(i *InlineTag) int {
			if i.Tag == Opener {
				return 2
			}
			return 1 + len(i.Content[0])
		},
	},
	Image: InlineHTMLTagset{
		Opener: func(i *InlineTag) string { return "" },
		Closer: func(i *InlineTag) string { return "" },
		OpLen: func(i *InlineTag) int {
			if i.Tag == Opener {
				return len(i.Content[0]) + 2
			}

			return 0
		},
	},
	LineBreak: InlineHTMLTagset{
		Opener: func(i *InlineTag) string { return "<br>" },
		Closer: func(i *InlineTag) string { return "" },
		OpLen: func(i *InlineTag) int {
			if len(i.Content) > 0 && i.Tag == Opener {
				return 1
			}
			return 0
		},
	},
}

Functions

func NewDocumentConstructor added in v0.1.1

func NewDocumentConstructor(i io.Reader) *documentConstructor

func Open

func Open(p string) (*documentConstructor, error)

Types

type BlockHTMLRender

type BlockHTMLRender struct {
	Opener       string
	Closer       string
	CleanContent bool
}

type BlockObject

type BlockObject struct {
	Type        BlockObjectType
	Initialized bool            // indicates if a block object is currently being initalized or not
	Content     string          // the content of a block object, in string format. This is the raw string parsed by a constructor.
	Inlines     []*InlineObject // whatever inline objects a block object may have
	Blocks      []*BlockObject  // some blocks can contain more blocks - if so, then a blockobject will contain these blocks.
}

func NewBlockObject

func NewBlockObject(t BlockObjectType, i bool) *BlockObject

func NewGeneric

func NewGeneric() *BlockObject

func NewHeader

func NewHeader() *BlockObject

func NewList

func NewList(p int, o string, r ListOrder) *BlockObject

func NewListItem

func NewListItem() *BlockObject

func NewParagraph

func NewParagraph() *BlockObject

func NewThematicBreak

func NewThematicBreak() *BlockObject

func (*BlockObject) GetListAttrib

func (b *BlockObject) GetListAttrib() ListAttribs

listOffsets are encoded in the List's content as fieldless CSV

type BlockObjectRender

type BlockObjectRender struct {
	Object *BlockObject
	Offset int

	Render string
	// contains filtered or unexported fields
}

type BlockObjectType

type BlockObjectType int
const (
	Generic BlockObjectType = iota // generics are placeholders
	Paragraph
	Header1
	Header2
	Header3
	Header4
	Header5
	Header6
	ThematicBreak
	List
	ListItem
	CodeBlock
)

type Document

type Document struct {
	Content []*BlockObject // A document is comprised of only blocks - so, this makes sense for accessing.
}

func (*Document) Render

func (d *Document) Render() string

type InlineContent

type InlineContent struct {
	HasContent bool
	Content    string
}

type InlineHTMLTag

type InlineHTMLTag func(*InlineTag) string

type InlineHTMLTagset

type InlineHTMLTagset struct {
	Opener InlineHTMLTag
	Closer InlineHTMLTag
	OpLen  InlineOpLen
}

type InlineObject

type InlineObject struct {
	Type     InlineObjectType
	StartPos int // indicates the current starting position of the object relative to content
	EndPos   int // indicates the ending position of the object relative to content
	Content  []string
	Closer   Specials // returns whatever closes the inline object
}

InlineObject represents an inline object within a block object. It consists of several things: - A start position, which indicates where in the content it should start - An end position, which indicates where in the content it should end.

Inline objects can be several types of things (bold, URL, emphasis, etc), so it is a method interface.

func NewEmText

func NewEmText(p int) *InlineObject

func NewGenericInline

func NewGenericInline(p int) *InlineObject

func NewInline

func NewInline(p int, t InlineObjectType) *InlineObject

func NewStrongText

func NewStrongText(p int) *InlineObject

type InlineObjectType

type InlineObjectType int
const (
	GenericInline InlineObjectType = iota
	StrongText
	LineBreak
	EmText
	LinkRef
	Link
	ImageRef
	Image
	CodeSpan
)

type InlineOpLen

type InlineOpLen func(*InlineTag) int

type InlineSort

type InlineSort struct{ InlineTags }

func (InlineSort) Less

func (s InlineSort) Less(i, j int) bool

type InlineTag

type InlineTag struct {
	Type    InlineObjectType
	Content []string
	Tag     InlineTagType
	Pos     int
}

type InlineTagType

type InlineTagType int
const (
	Opener InlineTagType = 0
	Closer InlineTagType = 1
)

type InlineTags

type InlineTags []*InlineTag

func (InlineTags) Len

func (t InlineTags) Len() int

func (InlineTags) Swap

func (t InlineTags) Swap(i, j int)

type ListAttribs

type ListAttribs struct {
	Pre   int
	Op    string
	Order ListOrder
}

type ListOrder

type ListOrder int
const (
	Unordered ListOrder = 0
	Ordered   ListOrder = 1
)

type SpecialAction

type SpecialAction func(*documentConstructor, *list.Element)

type Specials

type Specials int
const (
	DoubleNewLine Specials = iota
	DoubleAsterisk
	NewLine
	Asterisk
	None
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL