Skip to content

Commit

Permalink
feat(parser): implemented lists
Browse files Browse the repository at this point in the history
this commit implements list parsing as well as breaks (<br>). Currently nesting lists is not supported
  • Loading branch information
xNaCly committed Apr 14, 2023
1 parent 2530648 commit 79560a4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 27 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ Until a release is ready please build from source.
> Read more about fleck's usage [here](./doc/Usage.md)
- Follow the guide [above](#install) for installing fleck

- Run `fleck` from the cli on a Markdown file of your choice, for example the README of this project:

```bash
fleck README.md
```

- View the output:

![Readme.png](./doc/assets/README.png)
- View the output: ![Readme.png](./doc/assets/README.png)
2 changes: 0 additions & 2 deletions fleck.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build !bare

package main

import (
Expand Down
77 changes: 57 additions & 20 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,70 @@ func (p *Parser) tag() Tag {
func (p *Parser) list() Tag {
// TODO: parse checkmark unordered list

// skip the first -
// skip the first
p.advance()

if p.match(scanner.DASH, scanner.DASH) {
return Ruler{}
if p.check(scanner.DASH) {
p.advance()
if p.check(scanner.DASH) {
return Ruler{}
}
}

return p.paragraph()
// BUG: this is a mess, try to fix this
children := make([]Tag, 0)
curLine := make([]Tag, 0)

// TODO: this isn't finished, deactiving it temporarily
// paragraph should only contain inline code, italic and bold or text
for !p.check(scanner.EMPTYLINE) && !p.isAtEnd() {
// this is the next li
// TODO: no nesting supported, maybe implement that
if p.check(scanner.DASH) {
if len(curLine) != 0 {
children = append(children, ListItem{
children: curLine,
})
curLine = make([]Tag, 0)
}
p.advance()
}

// children := make([]Tag, 0)
// currentLine := make([]Tag, 0)
switch p.peek().Kind {
case scanner.STRAIGHTBRACEOPEN:
curLine = append(curLine, p.link())
case scanner.BANG:
// INFO: p.img automatically skips the new line
curLine = append(curLine, p.img())
case scanner.BACKTICK:
curLine = append(curLine, p.code(false))
case scanner.NEWLINE:
if len(curLine) != 0 {
curLine = append(curLine, Br{})
}
p.advance()
case scanner.STAR, scanner.UNDERSCORE:
curLine = append(curLine, p.emphasis())
case scanner.TEXT:
curLine = append(curLine, Text{content: p.peek().Value})
p.advance()
default:
curLine = append(curLine, Text{content: string(scanner.TOKEN_SYMBOL_MAP[p.peek().Kind])})
p.advance()
}
}

// for !p.check(scanner.EMPTYLINE) && !p.isAtEnd() {
// if p.check(scanner.DASH) {
// currentLine = make([]Tag, 0)
// children = append(children, ListItem{
// children: currentLine,
// })
// p.advance()
// }
// currentLine = append(currentLine, p.paragraph())
// }
if len(curLine) != 0 {
children = append(children, ListItem{
children: curLine,
})
curLine = make([]Tag, 0)
}

if len(children) == 0 {
return nil
}

// return List{
// children: children,
// }
return List{children: children}
}

// parses blockquotes
Expand Down Expand Up @@ -389,6 +425,7 @@ func (p *Parser) paragraph() Tag {
p.advance()
}
}

// skip the newline
p.advance()
if len(children) == 0 {
Expand Down
6 changes: 6 additions & 0 deletions parser/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ type Tag interface {
String() string
}

type Br struct{}

func (p Br) String() string {
return "</br>"
}

// <p></p> html paragraph
type Paragraph struct {
children []Tag
Expand Down
8 changes: 7 additions & 1 deletion test.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,13 @@ this is also not a link]()
###### Heading 6
- list
- list this is a very long line, possibly even too long, SAY WHATTTT
test test test loong shit [link](google.com) `inline code`, **bold**, _italic_ and even
arbitrary
line
breaks
wtf
- list 2, this li even includes an image ![this includes an image](https://avatars.githubusercontent.com/u/47723417?v=4)
- [x] checked list
- [ ] unchecked list
Expand Down

0 comments on commit 79560a4

Please sign in to comment.