aboutsummaryrefslogtreecommitdiff
path: root/doc.go
diff options
context:
space:
mode:
authorGuangxiong Lin <[email protected]>2023-01-24 20:27:22 +0800
committerGuangxiong Lin <[email protected]>2023-01-24 20:27:22 +0800
commit104ee66f5930f4a37ac84538c29a291bf1d08f4f (patch)
treec663768184594207fd68533a773740d99956c865 /doc.go
parent3f6aeea8c795b8bff59c1a8b45700d7bc44da4b9 (diff)
downloadzk-104ee66f5930f4a37ac84538c29a291bf1d08f4f.tar.gz
zk-104ee66f5930f4a37ac84538c29a291bf1d08f4f.tar.bz2
zk-104ee66f5930f4a37ac84538c29a291bf1d08f4f.zip
Use cobra as command selector
Diffstat (limited to 'doc.go')
-rw-r--r--doc.go154
1 files changed, 0 insertions, 154 deletions
diff --git a/doc.go b/doc.go
deleted file mode 100644
index 4971e8e..0000000
--- a/doc.go
+++ /dev/null
@@ -1,154 +0,0 @@
-package main
-
-import (
- "bytes"
- "io/fs"
- "net/url"
- "os"
- "path/filepath"
- "strings"
-
- "github.com/yuin/goldmark"
- "github.com/yuin/goldmark/ast"
- "github.com/yuin/goldmark/extension"
- "github.com/yuin/goldmark/parser"
- "github.com/yuin/goldmark/text"
-)
-
-var md = goldmark.New(
- goldmark.WithExtensions(extension.TaskList),
-)
-
-type Doc struct {
- path string
- content []byte
- links Set[string]
- backlinks Set[string]
-}
-
-func (doc *Doc) UpdateLinks(source, target string) {
- dirname := filepath.Dir(doc.path)
- relpath, err := filepath.Rel(dirname, target)
- if err != nil {
- return
- }
-
- for link := range doc.links {
- if filepath.Join(dirname, link) != source {
- continue
- }
-
- doc.content = bytes.Replace(doc.content, []byte(link), []byte(relpath), -1)
- // TODO: Update doc.links
- }
-
- doc.Overwrite()
-}
-
-func (doc *Doc) Overwrite() {
- os.WriteFile(doc.path, doc.content, 0644)
-}
-
-func NewDoc(filename string) *Doc {
- // NOTE: so far, only markdown is supported
- if !strings.HasSuffix(filename, ".md") {
- return nil
- }
-
- content, err := os.ReadFile(filename)
- if err != nil {
- return nil
- }
-
- root := md.Parser().Parse(
- text.NewReader(content),
- parser.WithContext(parser.NewContext()),
- )
-
- links := Set[string]{}
- err = ast.Walk(root, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
- if entering {
- switch link := n.(type) {
- case *ast.Link:
- href, err := url.PathUnescape(string(link.Destination))
- if err != nil {
- return ast.WalkStop, err
- }
-
- links.Insert(href)
- }
- }
- return ast.WalkContinue, nil
- })
-
- if err != nil {
- return nil
- }
-
- return &Doc{
- path: filename,
- content: content,
- links: links,
- backlinks: Set[string]{},
- }
-}
-
-type Docs Map[string, *Doc]
-
-func (docs Docs) GetOrNew(filename string) *Doc {
- if doc, ok := docs[filename]; ok {
- return doc
- }
-
- doc := NewDoc(filename)
- docs[filename] = doc
- return doc
-}
-
-func NewDocs(dirname string) Docs {
- docs := Docs{}
-
- dirname, err := filepath.Abs(dirname)
- if err != nil {
- return nil
- }
-
- if err := filepath.Walk(dirname, func(path string, info fs.FileInfo, err error) error {
- if err != nil {
- return err
- }
-
- // TODO: Add skip dir config
- if info.IsDir() && (info.Name() == ".git" || info.Name() == "vendor") {
- return filepath.SkipDir
- }
-
- if doc := NewDoc(path); doc != nil {
- docs[path] = doc
- }
-
- return nil
- }); err != nil {
- return nil
- }
-
- for filename, doc := range docs {
- for link := range doc.links {
- abslink := filepath.Join(filepath.Dir(filename), link)
- if _, ok := docs[abslink]; !ok {
- continue
- }
-
- docs[abslink].backlinks.Insert(filename)
- }
- }
-
- return docs
-}
-
-func (docs Docs) Contain(filename string) bool {
- _, ok := docs[filename]
- return ok
-}
-
-var DocCollection Docs