Skip to content

Commit

Permalink
Fix some vet errors, Improve attributes on ATXHeadings
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed May 16, 2019
1 parent e481813 commit 1963434
Show file tree
Hide file tree
Showing 10 changed files with 2,190 additions and 2,153 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Yusuke Inuzuka

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ goldmark
[![http://godoc.org/github.com/yuin/goldmark](https://godoc.org/github.com/yuin/goldmark?status.svg)](http://godoc.org/github.com/yuin/goldmark)
[![https://travis-ci.org/yuin/goldmark](https://travis-ci.org/yuin/goldmark.svg)](https://travis-ci.org/yuin/goldmark)
[![https://coveralls.io/r/yuin/goldmark](https://coveralls.io/repos/yuin/goldmark/badge.svg)](https://coveralls.io/r/yuin/goldmark)
[![https://goreportcard.com/report/github.com/yuin/goldmark](https://goreportcard.com/badge/github.com/yuin/goldmark)](https://goreportcard.com/report/github.com/yuin/goldmark)

> A markdown parser written in Go. Easy to extend, standard compliant, well structured.
Expand Down Expand Up @@ -128,10 +129,17 @@ Parser and Renderer options

Currently only headings support attributes.

**Attributes are being discussed in the
[CommonMark fourum](https://talk.commonmark.org/t/consistent-attribute-syntax/272).
This syntax possibly changes in the future. **


#### Headings

```
## heading ## {#id .className attrName=attrValue class="class1 class2"}
## heading {#id .className attrName=attrValue class="class1 class2"}
```

```
Expand Down
25 changes: 22 additions & 3 deletions parser/atx_heading.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,14 @@ func (b *atxHeadingParser) Continue(node ast.Node, reader text.Reader, pc Contex
}

func (b *atxHeadingParser) Close(node ast.Node, reader text.Reader, pc Context) {
if !b.AutoHeadingID {
return
if b.Attribute {
_, ok := node.AttributeString("id")
if !ok {
parseLastLineAttributes(node, reader, pc)
}
}
if !b.Attribute {

if b.AutoHeadingID {
_, ok := node.AttributeString("id")
if !ok {
generateAutoHeadingID(node.(*ast.Heading), reader, pc)
Expand All @@ -196,3 +200,18 @@ func generateAutoHeadingID(node *ast.Heading, reader text.Reader, pc Context) {
headingID := pc.IDs().Generate(line, attrAutoHeadingIDPrefix)
node.SetAttribute(attrNameID, headingID)
}

func parseLastLineAttributes(node ast.Node, reader text.Reader, pc Context) {
lastIndex := node.Lines().Len() - 1
lastLine := node.Lines().At(lastIndex)
line := lastLine.Value(reader.Source())
indicies := util.FindAttributeIndiciesReverse(line, true)
if indicies != nil {
for _, index := range indicies {
node.SetAttribute(line[index[0]:index[1]], line[index[2]:index[3]])
}
lastLine.Stop = lastLine.Start + indicies[0][0] - 1
lastLine.TrimRightSpace(reader.Source())
node.Lines().Set(lastIndex, lastLine)
}
}
4 changes: 2 additions & 2 deletions parser/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var defaultListParser = &listParser{}

// NewListParser returns a new BlockParser that
// parses lists.
// This parser must take predecence over the ListItemParser.
// This parser must take precedence over the ListItemParser.
func NewListParser() BlockParser {
return defaultListParser
}
Expand Down Expand Up @@ -157,7 +157,7 @@ func (b *listParser) Continue(node ast.Node, reader text.Reader, pc Context) Sta
}
return Continue | HasChildren
}
// Themantic Breaks take predecence over lists
// Themantic Breaks take precedence over lists
if isThemanticBreak(line) {
isHeading := false
last := pc.LastOpenedBlock().Node
Expand Down
4 changes: 2 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (s *ids) Put(value []byte) {
s.values[util.BytesToReadOnlyString(value)] = true
}

// ContextKey is a key that is used to set arbitary values to the context.
// ContextKey is a key that is used to set arbitrary values to the context.
type ContextKey int

// ContextKeyMax is a maximum value of the ContextKey.
Expand Down Expand Up @@ -629,7 +629,7 @@ func (o *withOption) SetParserOption(c *Config) {
}

// WithOption is a functional option that allow you to set
// an arbitary option to the parser.
// an arbitrary option to the parser.
func WithOption(name OptionName, value interface{}) Option {
return &withOption{name, value}
}
Expand Down
13 changes: 1 addition & 12 deletions parser/setext_headings.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,7 @@ func (b *setextHeadingParser) Close(node ast.Node, reader text.Reader, pc Contex
}

if b.Attribute {
lastIndex := node.Lines().Len() - 1
lastLine := node.Lines().At(lastIndex)
line := lastLine.Value(reader.Source())
indicies := util.FindAttributeIndiciesReverse(line, true)
if indicies != nil {
for _, index := range indicies {
node.SetAttribute(line[index[0]:index[1]], line[index[2]:index[3]])
}
lastLine.Stop = lastLine.Start + indicies[0][0] - 1
lastLine.TrimRightSpace(reader.Source())
node.Lines().Set(lastIndex, lastLine)
}
parseLastLineAttributes(node, reader, pc)
}

if b.AutoHeadingID {
Expand Down
2 changes: 1 addition & 1 deletion renderer/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func (d *defaultWriter) Write(writer util.BufWriter, source []byte) {
}
escaped = false
}
d.RawWrite(writer, source[n:len(source)])
d.RawWrite(writer, source[n:])
}

// DefaultWriter is a default implementation of the Writer.
Expand Down
2 changes: 1 addition & 1 deletion renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (o *withOption) SetConfig(c *Config) {
}

// WithOption is a functional option that allow you to set
// an arbitary option to the parser.
// an arbitrary option to the parser.
func WithOption(name OptionName, value interface{}) Option {
return &withOption{name, value}
}
Expand Down
Loading

0 comments on commit 1963434

Please sign in to comment.