Skip to content

Commit

Permalink
Markdown parser (#103)
Browse files Browse the repository at this point in the history
* add markdown parser(簡易版)

* modify for wasm
  • Loading branch information
ddddddO authored Nov 4, 2022
1 parent 6893a74 commit 03f4ec4
Show file tree
Hide file tree
Showing 16 changed files with 605 additions and 165 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ lint: sweep

test: sweep
go clean -testcache
go test . -race -v
go test . -race -v -count=1
go test ./markdown/... -race -v -count=1

cyclo: sweep
gocyclo .
Expand Down
2 changes: 1 addition & 1 deletion cmd/gtree-wasm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ editcss:
npx tailwindcss -i ./tailwind_base.css -o ./main.css --watch

serve:
python3 -m http.server 8080
cd ../../docs && python3 -m http.server 8080

confirm:
./confirm.sh
Expand Down
66 changes: 33 additions & 33 deletions cmd/gtree-wasm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,39 @@ <h1 class="text-3xl font-bold">
<div id="main" class="md:flex flex-row">
<div class="basis-6/12">
<textarea class="bg-teal-800 rounded-md mt-1 w-full h-64" id="in" spellcheck="false">- Output tree from Markdown.
- 手作業で枝を組み立てるのは面倒、
- 楽にツリー(treeコマンド(Linux/Windows)結果のようなテキスト)
- を作りたい時に役に立ちます。
- 枝を変えられます。
- Primate
- Strepsirrhini
- Lemuriformes
- Lemuroidea
- Cheirogaleidae
- Indriidae
- Lemuridae
- Lepilemuridae
- Daubentonioidea
- Daubentoniidae
- Lorisiformes
- Galagidae
- Lorisidae
- Haplorrhini
- Tarsiiformes
- Tarsiidae
- Simiiformes
- Platyrrhini
- Ceboidea
- Atelidae
- Cebidae
- Pithecioidea
- Pitheciidae
- Catarrhini
- Cercopithecoidea
- Cercopithecidae
- Hominoidea
- Hylobatidae
- Hominidae</textarea>
* 手作業で枝を組み立てるのは面倒、
+ 楽にツリー(treeコマンド(Linux/Windows)結果のようなテキスト)
+ を作りたい時に役に立ちます。
* 枝を変えられます。
# Primate
+ Strepsirrhini
+ Lemuriformes
- Lemuroidea
- Cheirogaleidae
- Indriidae
- Lemuridae
- Lepilemuridae
* Daubentonioidea
* Daubentoniidae
- Lorisiformes
- Galagidae
- Lorisidae
+ Haplorrhini
- Tarsiiformes
- Tarsiidae
- Simiiformes
- Platyrrhini
- Ceboidea
- Atelidae
- Cebidae
- Pithecioidea
- Pitheciidae
- Catarrhini
- Cercopithecoidea
- Cercopithecidae
- Hominoidea
- Hylobatidae
- Hominidae</textarea>

<button class="hover:outline rounded bg-cyan-600 shadow-xl px-1 mt-2" onClick="clearMarkdown();" id="clearMarkdown">clear</button>

Expand Down
2 changes: 1 addition & 1 deletion cmd/gtree-wasm/service_worker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// copied by https://laboradian.com/create-offline-site-using-sw/

// TODO: ファイル変更したらCACHE_VERSIONを変えてデプロイすること
const CACHE_VERSION = 'v1.0.8';
const CACHE_VERSION = 'v1.1.0';
const CACHE_NAME = `${registration.scope}!${CACHE_VERSION}`;

// キャッシュするファイルをセットする
Expand Down
66 changes: 33 additions & 33 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,39 @@ <h1 class="text-3xl font-bold">
<div id="main" class="md:flex flex-row">
<div class="basis-6/12">
<textarea class="bg-teal-800 rounded-md mt-1 w-full h-64" id="in" spellcheck="false">- Output tree from Markdown.
- 手作業で枝を組み立てるのは面倒、
- 楽にツリー(treeコマンド(Linux/Windows)結果のようなテキスト)
- を作りたい時に役に立ちます。
- 枝を変えられます。
- Primate
- Strepsirrhini
- Lemuriformes
- Lemuroidea
- Cheirogaleidae
- Indriidae
- Lemuridae
- Lepilemuridae
- Daubentonioidea
- Daubentoniidae
- Lorisiformes
- Galagidae
- Lorisidae
- Haplorrhini
- Tarsiiformes
- Tarsiidae
- Simiiformes
- Platyrrhini
- Ceboidea
- Atelidae
- Cebidae
- Pithecioidea
- Pitheciidae
- Catarrhini
- Cercopithecoidea
- Cercopithecidae
- Hominoidea
- Hylobatidae
- Hominidae</textarea>
* 手作業で枝を組み立てるのは面倒、
+ 楽にツリー(treeコマンド(Linux/Windows)結果のようなテキスト)
+ を作りたい時に役に立ちます。
* 枝を変えられます。
# Primate
+ Strepsirrhini
+ Lemuriformes
- Lemuroidea
- Cheirogaleidae
- Indriidae
- Lemuridae
- Lepilemuridae
* Daubentonioidea
* Daubentoniidae
- Lorisiformes
- Galagidae
- Lorisidae
+ Haplorrhini
- Tarsiiformes
- Tarsiidae
- Simiiformes
- Platyrrhini
- Ceboidea
- Atelidae
- Cebidae
- Pithecioidea
- Pitheciidae
- Catarrhini
- Cercopithecoidea
- Cercopithecidae
- Hominoidea
- Hylobatidae
- Hominidae</textarea>

<button class="hover:outline rounded bg-cyan-600 shadow-xl px-1 mt-2" onClick="clearMarkdown();" id="clearMarkdown">clear</button>

Expand Down
Binary file modified docs/main.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/service_worker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// copied by https://laboradian.com/create-offline-site-using-sw/

// TODO: ファイル変更したらCACHE_VERSIONを変えてデプロイすること
const CACHE_VERSION = 'v1.0.8';
const CACHE_VERSION = 'v1.1.0';
const CACHE_NAME = `${registration.scope}!${CACHE_VERSION}`;

// キャッシュするファイルをセットする
Expand Down
14 changes: 14 additions & 0 deletions markdown/markdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package markdown

type Markdown struct {
hierarchy uint
text string
}

func (m *Markdown) Hierarchy() uint {
return m.hierarchy
}

func (m *Markdown) Text() string {
return m.text
}
170 changes: 170 additions & 0 deletions markdown/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package markdown

import (
"errors"
"strings"
)

// 一旦簡易なパーサー
type Parser struct {
// rootが#要素かフラグ
// #であれば次の#までのhierarchyは+=1
isSharpRoot bool
spaces int
sep string
}

// TODO: 要リファクタ
func NewParser(spaces int) *Parser {
sep := space
if spaces == 1 {
sep = tab
}

return &Parser{
spaces: spaces,
sep: sep,
}
}

const (
sharp = "#"

hyphen = "-"
asterisk = "*"
plus = "+"

space = " "
tab = "\t"
)

const (
rootHierarchyNum uint = 1
)

var (
ErrBlankLine = errors.New("blank line")
ErrEmptyText = errors.New("empty text")
ErrIncorrectFormat = errors.New("incorrect input format")
)

// TODO: 要リファクタ
func (p *Parser) Parse(row string) (*Markdown, error) {
// 空行か否か
if p.isEmpty(row) {
return nil, ErrBlankLine
}

if strings.HasPrefix(row, sharp) {
p.isSharpRoot = true

_, after, found := strings.Cut(row, sharp)
if !found {
return nil, ErrIncorrectFormat
}
text := strings.TrimLeft(after, sharp)
text = strings.Trim(text, space)
if len(text) == 0 {
return nil, ErrEmptyText
}

return &Markdown{
hierarchy: rootHierarchyNum,
text: text,
}, nil
}

// #のセクション内
if p.isSharpRoot {
spaceCount, afterText, err := p.separateRow(row)
if err != nil {
return nil, err
}

text := strings.TrimPrefix(afterText, space)
if len(text) == 0 {
return nil, ErrEmptyText
}

hierarchy := p.calculateHierarchy(spaceCount)
return &Markdown{
hierarchy: hierarchy,
text: text,
}, nil
}

spaceCount, afterText, err := p.separateRow(row)
if err != nil {
return nil, err
}

text := strings.TrimPrefix(afterText, space)
if len(text) == 0 {
return nil, ErrEmptyText
}

hierarchy := p.calculateHierarchy(spaceCount)
return &Markdown{
hierarchy: hierarchy,
text: text,
}, nil
}

func (p *Parser) isEmpty(row string) bool {
r := strings.TrimSpace(row)
return len(r) == 0
}

func (p *Parser) validSpaces(spaceCount int) error {
if p.isTab() {
return nil
}
if spaceCount%p.spaces != 0 {
return ErrIncorrectFormat
}
return nil
}

func (p *Parser) calculateHierarchy(spaceCount int) uint {
var hierarchy uint
if p.isTab() {
hierarchy = uint(spaceCount) + rootHierarchyNum
} else {
hierarchy = uint(spaceCount/p.spaces) + rootHierarchyNum
}

if p.isSharpRoot {
hierarchy += 1
}
return hierarchy
}

func (p *Parser) isTab() bool {
return p.spaces == 1
}

var listSymbols = []string{hyphen, asterisk, plus}

func (p *Parser) separateRow(row string) (int, string, error) {
var err error
for _, symbol := range listSymbols {
before, after, found := strings.Cut(row, symbol)
if !found {
err = ErrIncorrectFormat
continue
}
spaceCount := strings.Count(before, p.sep)
if spaceCount != len(before) {
err = ErrIncorrectFormat
continue
}
if e := p.validSpaces(spaceCount); e != nil {
err = e
continue
}

return spaceCount, after, nil
}

return 0, "", err
}
Loading

0 comments on commit 03f4ec4

Please sign in to comment.