Skip to content

Commit

Permalink
for wasm impl (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddddddO authored Apr 15, 2023
1 parent c7590ed commit 329b12c
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 94 deletions.
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.1.4';
const CACHE_VERSION = 'v1.1.5';
const CACHE_NAME = `${registration.scope}!${CACHE_VERSION}`;

// キャッシュするファイルをセットする
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.1.4';
const CACHE_VERSION = 'v1.1.5';
const CACHE_NAME = `${registration.scope}!${CACHE_VERSION}`;

// キャッシュするファイルをセットする
Expand Down
2 changes: 2 additions & 0 deletions root_generator.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !wasm

package gtree

import (
Expand Down
2 changes: 2 additions & 0 deletions tree.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !wasm

package gtree

import (
Expand Down
2 changes: 2 additions & 0 deletions tree_grower.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !wasm

package gtree

import (
Expand Down
2 changes: 2 additions & 0 deletions tree_handler.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !wasm

package gtree

import (
Expand Down
2 changes: 2 additions & 0 deletions tree_handler_programmably.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !wasm

package gtree

import (
Expand Down
2 changes: 2 additions & 0 deletions tree_mkdirer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !wasm

package gtree

import (
Expand Down
55 changes: 55 additions & 0 deletions wasm_root_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build wasm

package gtree

import (
"bufio"
"io"
)

type rootGenerator struct {
counter *counter
scanner *bufio.Scanner
nodeGenerator *nodeGenerator
}

func newRootGenerator(r io.Reader, st spaceType) *rootGenerator {
return &rootGenerator{
counter: newCounter(),
scanner: bufio.NewScanner(r),
nodeGenerator: newNodeGenerator(st),
}
}

func (rg *rootGenerator) generate() ([]*Node, error) {
var (
stack *stack
roots []*Node
)

for rg.scanner.Scan() {
currentNode, err := rg.nodeGenerator.generate(rg.scanner.Text(), rg.counter.next())
if err != nil {
return nil, err
}
if currentNode == nil {
continue
}

if currentNode.isRoot() {
rg.counter.reset()
roots = append(roots, currentNode)
stack = newStack()
stack.push(currentNode)
continue
}

if stack == nil {
return nil, errNilStack
}

stack.dfs(currentNode)
}

return roots, rg.scanner.Err()
}
64 changes: 64 additions & 0 deletions wasm_tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//go:build wasm

package gtree

import (
"io"
)

type tree struct {
roots []*Node

grower grower
spreader spreader
}

// 関心事は各ノードの枝の形成
type grower interface {
grow([]*Node) error
enableValidation()
}

// 関心事はtreeの出力
type spreader interface {
spread(io.Writer, []*Node) error
}

func newTree(conf *config, roots []*Node) *tree {
growerFactory := func(lastNodeFormat, intermedialNodeFormat branchFormat, dryrun bool, encode encode) grower {
if encode != encodeDefault {
return newNopGrower()
}
return newGrower(lastNodeFormat, intermedialNodeFormat, dryrun)
}

spreaderFactory := func(encode encode, dryrun bool, fileExtensions []string) spreader {
if dryrun {
return newColorizeSpreader(fileExtensions)
}
return newSpreader(encode)
}

return &tree{
roots: roots,
grower: growerFactory(
conf.lastNodeFormat,
conf.intermedialNodeFormat,
conf.dryrun,
conf.encode,
),
spreader: spreaderFactory(
conf.encode,
conf.dryrun,
conf.fileExtensions,
),
}
}

func (t *tree) grow() error {
return t.grower.grow(t.roots)
}

func (t *tree) spread(w io.Writer) error {
return t.spreader.spread(w, t.roots)
}
130 changes: 130 additions & 0 deletions wasm_tree_grower.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//go:build wasm

package gtree

func newGrower(
lastNodeFormat, intermedialNodeFormat branchFormat,
enabledValidation bool,
) grower {
return &defaultGrower{
lastNodeFormat: lastNodeFormat,
intermedialNodeFormat: intermedialNodeFormat,
enabledValidation: enabledValidation,
}
}

func newNopGrower() grower {
return &nopGrower{}
}

type branchFormat struct {
directly, indirectly string
}

type defaultGrower struct {
lastNodeFormat branchFormat
intermedialNodeFormat branchFormat
enabledValidation bool
}

func (dg *defaultGrower) grow(roots []*Node) error {
for _, root := range roots {
if err := dg.assemble(root); err != nil {
return err
}
}
return nil
}

func (dg *defaultGrower) assemble(current *Node) error {
if err := dg.assembleBranch(current); err != nil {
return err
}

for _, child := range current.children {
if err := dg.assemble(child); err != nil {
return err
}
}
return nil
}

func (dg *defaultGrower) assembleBranch(current *Node) error {
current.clean() // 例えば、MkdirProgrammably funcでrootノードを使いまわすと、前回func実行時に形成されたノードの枝が残ったまま追記されてしまうため。

dg.assembleBranchDirectly(current)

// go back to the root to form a branch.
tmpParent := current.parent
if tmpParent != nil {
for ; !tmpParent.isRoot(); tmpParent = tmpParent.parent {
dg.assembleBranchIndirectly(current, tmpParent)
}
}

dg.assembleBranchFinally(current, tmpParent)

if dg.enabledValidation {
return current.validatePath()
}
return nil
}

func (dg *defaultGrower) assembleBranchDirectly(current *Node) {
if current == nil || current.isRoot() {
return
}

current.setPath(current.name)

if current.isLastOfHierarchy() {
current.setBranch(current.branch(), dg.lastNodeFormat.directly)
} else {
current.setBranch(current.branch(), dg.intermedialNodeFormat.directly)
}
}

func (dg *defaultGrower) assembleBranchIndirectly(current, parent *Node) {
if current == nil || parent == nil || current.isRoot() {
return
}

current.setPath(parent.name, current.path())

if parent.isLastOfHierarchy() {
current.setBranch(dg.lastNodeFormat.indirectly, current.branch())
} else {
current.setBranch(dg.intermedialNodeFormat.indirectly, current.branch())
}
}

func (*defaultGrower) assembleBranchFinally(current, root *Node) {
if current == nil {
return
}

if root != nil {
current.setPath(root.path(), current.path())
}

if current.isRoot() {
current.setBranch(current.name, "\n")
} else {
current.setBranch(current.branch(), " ", current.name, "\n")
}
}

func (dg *defaultGrower) enableValidation() {
dg.enabledValidation = true
}

type nopGrower struct{}

func (*nopGrower) grow(_ []*Node) error { return nil }

func (*nopGrower) enableValidation() {}

var (
_ grower = (*defaultGrower)(nil)
_ grower = (*nopGrower)(nil)
)
27 changes: 27 additions & 0 deletions wasm_tree_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build wasm

package gtree

import (
"io"
)

// Output outputs a tree to w with r as Markdown format input.
func Output(w io.Writer, r io.Reader, options ...Option) error {
conf, err := newConfig(options)
if err != nil {
return err
}

rg := newRootGenerator(r, conf.space)
roots, err := rg.generate()
if err != nil {
return err
}

tree := newTree(conf, roots)
if err := tree.grow(); err != nil {
return err
}
return tree.spread(w)
}
Loading

0 comments on commit 329b12c

Please sign in to comment.