mirror of
https://github.com/kovetskiy/mark.git
synced 2025-01-23 09:13:49 +08:00
*: Reorganize code
This commit is contained in:
parent
2c71b50438
commit
dc8842106b
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,6 +1,5 @@
|
||||
/mark
|
||||
/docker
|
||||
/testdata
|
||||
.idea/
|
||||
/mark.test
|
||||
/profile.cov
|
||||
|
@ -11,8 +11,8 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/confluence"
|
||||
"github.com/kovetskiy/mark/pkg/mark/vfs"
|
||||
"github.com/kovetskiy/mark/confluence"
|
||||
"github.com/kovetskiy/mark/vfs"
|
||||
"github.com/reconquest/karma-go"
|
||||
"github.com/reconquest/pkg/log"
|
||||
)
|
@ -7,7 +7,7 @@ import (
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/mark/includes"
|
||||
"github.com/kovetskiy/mark/includes"
|
||||
"github.com/reconquest/karma-go"
|
||||
"github.com/reconquest/pkg/log"
|
||||
"github.com/reconquest/regexputil-go"
|
32
main.go
32
main.go
@ -11,13 +11,15 @@ import (
|
||||
|
||||
"github.com/bmatcuk/doublestar/v4"
|
||||
"github.com/kovetskiy/lorg"
|
||||
"github.com/kovetskiy/mark/pkg/confluence"
|
||||
"github.com/kovetskiy/mark/pkg/mark"
|
||||
"github.com/kovetskiy/mark/pkg/mark/attachment"
|
||||
"github.com/kovetskiy/mark/pkg/mark/includes"
|
||||
"github.com/kovetskiy/mark/pkg/mark/macro"
|
||||
"github.com/kovetskiy/mark/pkg/mark/stdlib"
|
||||
"github.com/kovetskiy/mark/pkg/mark/vfs"
|
||||
"github.com/kovetskiy/mark/attachment"
|
||||
"github.com/kovetskiy/mark/confluence"
|
||||
"github.com/kovetskiy/mark/includes"
|
||||
"github.com/kovetskiy/mark/macro"
|
||||
mark "github.com/kovetskiy/mark/markdown"
|
||||
"github.com/kovetskiy/mark/metadata"
|
||||
"github.com/kovetskiy/mark/page"
|
||||
"github.com/kovetskiy/mark/stdlib"
|
||||
"github.com/kovetskiy/mark/vfs"
|
||||
"github.com/reconquest/karma-go"
|
||||
"github.com/reconquest/pkg/log"
|
||||
"github.com/urfave/cli/v2"
|
||||
@ -307,7 +309,7 @@ func processFile(
|
||||
|
||||
parents := strings.Split(cCtx.String("parents"), cCtx.String("parents-delimiter"))
|
||||
|
||||
meta, markdown, err := mark.ExtractMeta(markdown, cCtx.String("space"), cCtx.Bool("title-from-h1"), parents)
|
||||
meta, markdown, err := metadata.ExtractMeta(markdown, cCtx.String("space"), cCtx.Bool("title-from-h1"), parents)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -386,15 +388,15 @@ func processFile(
|
||||
}
|
||||
}
|
||||
|
||||
links, err := mark.ResolveRelativeLinks(api, meta, markdown, filepath.Dir(file), cCtx.String("space"), cCtx.Bool("title-from-h1"), parents)
|
||||
links, err := page.ResolveRelativeLinks(api, meta, markdown, filepath.Dir(file), cCtx.String("space"), cCtx.Bool("title-from-h1"), parents)
|
||||
if err != nil {
|
||||
log.Fatalf(err, "unable to resolve relative links")
|
||||
}
|
||||
|
||||
markdown = mark.SubstituteLinks(markdown, links)
|
||||
markdown = page.SubstituteLinks(markdown, links)
|
||||
|
||||
if cCtx.Bool("dry-run") {
|
||||
_, _, err := mark.ResolvePage(cCtx.Bool("dry-run"), api, meta)
|
||||
_, _, err := page.ResolvePage(cCtx.Bool("dry-run"), api, meta)
|
||||
if err != nil {
|
||||
log.Fatalf(err, "unable to resolve page location")
|
||||
}
|
||||
@ -415,7 +417,7 @@ func processFile(
|
||||
var target *confluence.PageInfo
|
||||
|
||||
if meta != nil {
|
||||
parent, page, err := mark.ResolvePage(cCtx.Bool("dry-run"), api, meta)
|
||||
parent, page, err := page.ResolvePage(cCtx.Bool("dry-run"), api, meta)
|
||||
if err != nil {
|
||||
log.Fatalf(
|
||||
karma.Describe("title", meta.Title).Reason(err),
|
||||
@ -542,7 +544,7 @@ func processFile(
|
||||
return target
|
||||
}
|
||||
|
||||
func updateLabels(api *confluence.API, target *confluence.PageInfo, meta *mark.Meta) {
|
||||
func updateLabels(api *confluence.API, target *confluence.PageInfo, meta *metadata.Meta) {
|
||||
|
||||
labelInfo, err := api.GetPageLabels(target, "global")
|
||||
if err != nil {
|
||||
@ -579,7 +581,7 @@ func updateLabels(api *confluence.API, target *confluence.PageInfo, meta *mark.M
|
||||
}
|
||||
|
||||
// Page has label but label not in Metadata
|
||||
func determineLabelsToRemove(labelInfo *confluence.LabelInfo, meta *mark.Meta) []string {
|
||||
func determineLabelsToRemove(labelInfo *confluence.LabelInfo, meta *metadata.Meta) []string {
|
||||
var labels []string
|
||||
for _, label := range labelInfo.Labels {
|
||||
if !slices.ContainsFunc(meta.Labels, func(metaLabel string) bool {
|
||||
@ -592,7 +594,7 @@ func determineLabelsToRemove(labelInfo *confluence.LabelInfo, meta *mark.Meta) [
|
||||
}
|
||||
|
||||
// Metadata has label but Page does not have it
|
||||
func determineLabelsToAdd(meta *mark.Meta, labelInfo *confluence.LabelInfo) []string {
|
||||
func determineLabelsToAdd(meta *metadata.Meta, labelInfo *confluence.LabelInfo) []string {
|
||||
var labels []string
|
||||
for _, metaLabel := range meta.Labels {
|
||||
if !slices.ContainsFunc(labelInfo.Labels, func(label confluence.Label) bool {
|
||||
|
@ -2,12 +2,11 @@ package mark
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"regexp"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/mark/attachment"
|
||||
cparser "github.com/kovetskiy/mark/pkg/mark/parser"
|
||||
crenderer "github.com/kovetskiy/mark/pkg/mark/renderer"
|
||||
"github.com/kovetskiy/mark/pkg/mark/stdlib"
|
||||
"github.com/kovetskiy/mark/attachment"
|
||||
cparser "github.com/kovetskiy/mark/parser"
|
||||
crenderer "github.com/kovetskiy/mark/renderer"
|
||||
"github.com/kovetskiy/mark/stdlib"
|
||||
"github.com/reconquest/pkg/log"
|
||||
"github.com/yuin/goldmark"
|
||||
|
||||
@ -104,16 +103,4 @@ func CompileMarkdown(markdown []byte, stdlib *stdlib.Lib, path string, mermaidPr
|
||||
log.Tracef(nil, "rendered markdown to html:\n%s", string(html))
|
||||
|
||||
return string(html), confluenceExtension.Attachments
|
||||
|
||||
}
|
||||
|
||||
// ExtractDocumentLeadingH1 will extract leading H1 heading
|
||||
func ExtractDocumentLeadingH1(markdown []byte) string {
|
||||
h1 := regexp.MustCompile(`#[^#]\s*(.*)\s*\n`)
|
||||
groups := h1.FindSubmatch(markdown)
|
||||
if groups == nil {
|
||||
return ""
|
||||
} else {
|
||||
return string(groups[1])
|
||||
}
|
||||
}
|
@ -2,11 +2,13 @@ package mark
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/mark/stdlib"
|
||||
"github.com/kovetskiy/mark/stdlib"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -29,6 +31,13 @@ func loadData(t *testing.T, filename, variant string) ([]byte, string, []byte) {
|
||||
}
|
||||
|
||||
func TestCompileMarkdown(t *testing.T) {
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
dir := path.Join(path.Dir(filename), "..")
|
||||
err := os.Chdir(dir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
test := assert.New(t)
|
||||
|
||||
testcases, err := filepath.Glob("testdata/*.md")
|
||||
@ -48,6 +57,13 @@ func TestCompileMarkdown(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCompileMarkdownDropH1(t *testing.T) {
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
dir := path.Join(path.Dir(filename), "..")
|
||||
err := os.Chdir(dir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
test := assert.New(t)
|
||||
|
||||
testcases, err := filepath.Glob("testdata/*.md")
|
||||
@ -67,6 +83,13 @@ func TestCompileMarkdownDropH1(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCompileMarkdownStripNewlines(t *testing.T) {
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
dir := path.Join(path.Dir(filename), "..")
|
||||
err := os.Chdir(dir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
test := assert.New(t)
|
||||
|
||||
testcases, err := filepath.Glob("testdata/*.md")
|
||||
@ -84,16 +107,3 @@ func TestCompileMarkdownStripNewlines(t *testing.T) {
|
||||
test.EqualValues(string(html), actual, filename+" vs "+htmlname)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractDocumentLeadingH1(t *testing.T) {
|
||||
filename := "testdata/header.md"
|
||||
|
||||
markdown, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
actual := ExtractDocumentLeadingH1(markdown)
|
||||
|
||||
assert.Equal(t, "a", actual)
|
||||
}
|
@ -7,7 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
mermaid "github.com/dreampuf/mermaid.go"
|
||||
"github.com/kovetskiy/mark/pkg/mark/attachment"
|
||||
"github.com/kovetskiy/mark/attachment"
|
||||
"github.com/reconquest/pkg/log"
|
||||
)
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/mark/attachment"
|
||||
"github.com/kovetskiy/mark/attachment"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -1,4 +1,4 @@
|
||||
package mark
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@ -166,3 +166,14 @@ func ExtractMeta(data []byte, spaceFromCli string, titleFromH1 bool, parents []s
|
||||
|
||||
return meta, data[offset:], nil
|
||||
}
|
||||
|
||||
// ExtractDocumentLeadingH1 will extract leading H1 heading
|
||||
func ExtractDocumentLeadingH1(markdown []byte) string {
|
||||
h1 := regexp.MustCompile(`#[^#]\s*(.*)\s*\n`)
|
||||
groups := h1.FindSubmatch(markdown)
|
||||
if groups == nil {
|
||||
return ""
|
||||
} else {
|
||||
return string(groups[1])
|
||||
}
|
||||
}
|
30
metadata/metadata_test.go
Normal file
30
metadata/metadata_test.go
Normal file
@ -0,0 +1,30 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestExtractDocumentLeadingH1(t *testing.T) {
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
dir := path.Join(path.Dir(filename), "..")
|
||||
err := os.Chdir(dir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
filename = "testdata/header.md"
|
||||
|
||||
markdown, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
actual := ExtractDocumentLeadingH1(markdown)
|
||||
|
||||
assert.Equal(t, "a", actual)
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
package mark
|
||||
package page
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/confluence"
|
||||
"github.com/kovetskiy/mark/confluence"
|
||||
"github.com/reconquest/karma-go"
|
||||
"github.com/reconquest/pkg/log"
|
||||
)
|
@ -1,4 +1,4 @@
|
||||
package mark
|
||||
package page
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -8,7 +8,8 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/confluence"
|
||||
"github.com/kovetskiy/mark/confluence"
|
||||
"github.com/kovetskiy/mark/metadata"
|
||||
"github.com/reconquest/karma-go"
|
||||
"github.com/reconquest/pkg/log"
|
||||
"golang.org/x/tools/godoc/util"
|
||||
@ -27,7 +28,7 @@ type markdownLink struct {
|
||||
|
||||
func ResolveRelativeLinks(
|
||||
api *confluence.API,
|
||||
meta *Meta,
|
||||
meta *metadata.Meta,
|
||||
markdown []byte,
|
||||
base string,
|
||||
spaceFromCli string,
|
||||
@ -104,7 +105,7 @@ func resolveLink(
|
||||
|
||||
// This helps to determine if found link points to file that's
|
||||
// not markdown or have mark required metadata
|
||||
linkMeta, _, err := ExtractMeta(linkContents, spaceFromCli, titleFromH1, parents)
|
||||
linkMeta, _, err := metadata.ExtractMeta(linkContents, spaceFromCli, titleFromH1, parents)
|
||||
if err != nil {
|
||||
log.Errorf(
|
||||
err,
|
@ -1,4 +1,4 @@
|
||||
package mark
|
||||
package page
|
||||
|
||||
import (
|
||||
"testing"
|
@ -1,9 +1,10 @@
|
||||
package mark
|
||||
package page
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/confluence"
|
||||
"github.com/kovetskiy/mark/confluence"
|
||||
"github.com/kovetskiy/mark/metadata"
|
||||
"github.com/reconquest/karma-go"
|
||||
"github.com/reconquest/pkg/log"
|
||||
)
|
||||
@ -11,7 +12,7 @@ import (
|
||||
func ResolvePage(
|
||||
dryRun bool,
|
||||
api *confluence.API,
|
||||
meta *Meta,
|
||||
meta *metadata.Meta,
|
||||
) (*confluence.PageInfo, *confluence.PageInfo, error) {
|
||||
page, err := api.FindPage(meta.Space, meta.Title, meta.Type)
|
||||
if err != nil {
|
@ -3,7 +3,7 @@ package renderer
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/mark/stdlib"
|
||||
"github.com/kovetskiy/mark/stdlib"
|
||||
|
||||
"github.com/yuin/goldmark/ast"
|
||||
"github.com/yuin/goldmark/renderer"
|
@ -5,9 +5,9 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/mark/attachment"
|
||||
"github.com/kovetskiy/mark/pkg/mark/mermaid"
|
||||
"github.com/kovetskiy/mark/pkg/mark/stdlib"
|
||||
"github.com/kovetskiy/mark/attachment"
|
||||
"github.com/kovetskiy/mark/mermaid"
|
||||
"github.com/kovetskiy/mark/stdlib"
|
||||
"github.com/reconquest/pkg/log"
|
||||
|
||||
"github.com/yuin/goldmark/ast"
|
@ -3,7 +3,7 @@ package renderer
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/mark/stdlib"
|
||||
"github.com/kovetskiy/mark/stdlib"
|
||||
|
||||
"github.com/yuin/goldmark/ast"
|
||||
"github.com/yuin/goldmark/renderer"
|
@ -5,9 +5,9 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/mark/attachment"
|
||||
"github.com/kovetskiy/mark/pkg/mark/stdlib"
|
||||
"github.com/kovetskiy/mark/pkg/mark/vfs"
|
||||
"github.com/kovetskiy/mark/attachment"
|
||||
"github.com/kovetskiy/mark/stdlib"
|
||||
"github.com/kovetskiy/mark/vfs"
|
||||
|
||||
"github.com/yuin/goldmark/ast"
|
||||
"github.com/yuin/goldmark/renderer"
|
@ -4,8 +4,8 @@ import (
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/kovetskiy/mark/pkg/confluence"
|
||||
"github.com/kovetskiy/mark/pkg/mark/macro"
|
||||
"github.com/kovetskiy/mark/confluence"
|
||||
"github.com/kovetskiy/mark/macro"
|
||||
"github.com/reconquest/pkg/log"
|
||||
|
||||
"github.com/reconquest/karma-go"
|
Before Width: | Height: | Size: 3.9 KiB After Width: | Height: | Size: 3.9 KiB |
Loading…
Reference in New Issue
Block a user