Skip to content

Commit

Permalink
*: Reorganize code
Browse files Browse the repository at this point in the history
  • Loading branch information
mrueg committed Sep 28, 2024
1 parent 2c71b50 commit e53752b
Show file tree
Hide file tree
Showing 73 changed files with 115 additions and 1,115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/mark/macro/macro.go → macro/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
32 changes: 17 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
21 changes: 4 additions & 17 deletions pkg/mark/markdown.go → markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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])
}
}
40 changes: 25 additions & 15 deletions pkg/mark/markdown_test.go → markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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")
Expand All @@ -48,9 +57,16 @@ 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")
testcases, err := filepath.Glob("../testdata/*.md")
if err != nil {
panic(err)
}
Expand All @@ -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")
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/mark/mermaid/mermaid.go → mermaid/mermaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/kovetskiy/mark/pkg/mark/attachment"
"github.com/kovetskiy/mark/attachment"
"github.com/stretchr/testify/assert"
)

Expand Down
13 changes: 12 additions & 1 deletion pkg/mark/meta.go → metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mark
package metadata

import (
"bufio"
Expand Down Expand Up @@ -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 changes: 30 additions & 0 deletions metadata/metadata_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions pkg/mark/ancestry.go → page/ancestry.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down
9 changes: 5 additions & 4 deletions pkg/mark/link.go → page/link.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mark
package page

import (
"bytes"
Expand All @@ -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"
Expand All @@ -27,7 +28,7 @@ type markdownLink struct {

func ResolveRelativeLinks(
api *confluence.API,
meta *Meta,
meta *metadata.Meta,
markdown []byte,
base string,
spaceFromCli string,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/mark/link_test.go → page/link_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mark
package page

import (
"testing"
Expand Down
7 changes: 4 additions & 3 deletions pkg/mark/mark.go → page/page.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
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"
)

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 {
Expand Down
File renamed without changes.
20 changes: 0 additions & 20 deletions pkg/mark/testdata/codes-droph1.html

This file was deleted.

Loading

0 comments on commit e53752b

Please sign in to comment.