Skip to content

Commit

Permalink
add pseudo protocol attachment://
Browse files Browse the repository at this point in the history
  • Loading branch information
kovetskiy committed Apr 20, 2019
1 parent 2abc2f1 commit 51f28f8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 38 deletions.
23 changes: 16 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Options:
-k Lock page editing to current user only to prevent accidental
manual edits over Confluence Web UI.
--dry-run Show resulting HTML and don't update Confluence page content.
--debug Enable debug logs.
--trace Enable trace logs.
-h --help Show this screen and call 911.
-v --version Show version.
Expand All @@ -85,7 +86,7 @@ var (
log *cog.Logger
)

func initlog(trace bool) {
func initlog(debug, trace bool) {
stderr := lorg.NewLog()
stderr.SetIndentLines(true)
stderr.SetFormat(
Expand All @@ -94,9 +95,16 @@ func initlog(trace bool) {

log = cog.NewLogger(stderr)

if debug {
log.SetLevel(lorg.LevelDebug)
}

if trace {
log.SetLevel(lorg.LevelTrace)
}

mark.SetLogger(log)
confluence.SetLogger(log)
}

func main() {
Expand All @@ -109,10 +117,9 @@ func main() {
targetFile, _ = args["-f"].(string)
dryRun = args["--dry-run"].(bool)
editLock = args["-k"].(bool)
trace = args["--trace"].(bool)
)

initlog(trace)
initlog(args["--debug"].(bool), args["--trace"].(bool))

config, err := getConfig(filepath.Join(os.Getenv("HOME"), ".config/mark"))
if err != nil && !os.IsNotExist(err) {
Expand All @@ -129,10 +136,8 @@ func main() {
log.Fatal(err)
}

htmlData := mark.CompileMarkdown(markdownData)

if dryRun {
fmt.Println(string(htmlData))
fmt.Println(string(mark.CompileMarkdown(markdownData)))
os.Exit(0)
}

Expand Down Expand Up @@ -187,11 +192,15 @@ func main() {
target = page
}

err = mark.ResolveAttachments(api, target, ".", meta.Attachments)
attaches, err := mark.ResolveAttachments(api, target, ".", meta.Attachments)
if err != nil {
log.Fatalf(err, "unable to create/update attachments")
}

markdownData = mark.CompileAttachmentLinks(markdownData, attaches)

htmlData := mark.CompileMarkdown(markdownData)

err = api.UpdatePage(
target,
MacroLayout{meta.Layout, [][]byte{htmlData}}.Render(),
Expand Down
6 changes: 3 additions & 3 deletions pkg/confluence/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ func (api *API) UpdateAttachment(
return info, err
}

//if request.Raw.StatusCode != 200 {
return info, newErrorStatusNotOK(request)
//}
if request.Raw.StatusCode != 200 {
return info, newErrorStatusNotOK(request)
}

if len(result.Results) == 0 {
return info, errors.New(
Expand Down
2 changes: 1 addition & 1 deletion pkg/mark/ancestry.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func EnsureAncestry(
break
}

log.Tracef(nil, "parent page %q exists: %s", title, page.Links.Full)
log.Debugf(nil, "parent page %q exists: %s", title, page.Links.Full)

rest = ancestry[i:]
parent = page
Expand Down
82 changes: 57 additions & 25 deletions pkg/mark/attachment.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package mark

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"sort"
"strings"

"github.com/kovetskiy/mark/pkg/confluence"
Expand All @@ -32,8 +33,8 @@ func ResolveAttachments(
page *confluence.PageInfo,
base string,
names []string,
) error {
attachs := []Attachment{}
) ([]Attachment, error) {
attaches := []Attachment{}
for _, name := range names {
attach := Attachment{
Name: name,
Expand All @@ -43,15 +44,15 @@ func ResolveAttachments(

checksum, err := getChecksum(attach.Path)
if err != nil {
return karma.Format(
return nil, karma.Format(
err,
"unable to get checksum for attachment: %q", attach.Name,
)
}

attach.Checksum = checksum

attachs = append(attachs, attach)
attaches = append(attaches, attach)
}

remotes, err := api.GetAttachments(page.ID)
Expand All @@ -62,7 +63,7 @@ func ResolveAttachments(
existing := []Attachment{}
creating := []Attachment{}
updating := []Attachment{}
for _, attach := range attachs {
for _, attach := range attaches {
var found bool
var same bool
for _, remote := range remotes {
Expand Down Expand Up @@ -92,21 +93,6 @@ func ResolveAttachments(
}
}

{
marshaledXXX, _ := json.MarshalIndent(existing, "", " ")
fmt.Printf("existing: %s\n", string(marshaledXXX))
}

{
marshaledXXX, _ := json.MarshalIndent(creating, "", " ")
fmt.Printf("creating: %s\n", string(marshaledXXX))
}

{
marshaledXXX, _ := json.MarshalIndent(updating, "", " ")
fmt.Printf("updating: %s\n", string(marshaledXXX))
}

for i, attach := range creating {
log.Infof(nil, "creating attachment: %q", attach.Name)

Expand All @@ -117,7 +103,7 @@ func ResolveAttachments(
attach.Path,
)
if err != nil {
return karma.Format(
return nil, karma.Format(
err,
"unable to create attachment %q",
attach.Name,
Expand All @@ -141,7 +127,7 @@ func ResolveAttachments(
attach.Path,
)
if err != nil {
return karma.Format(
return nil, karma.Format(
err,
"unable to update attachment %q",
attach.Name,
Expand All @@ -153,7 +139,53 @@ func ResolveAttachments(
updating[i] = attach
}

return nil
attaches = []Attachment{}
attaches = append(attaches, existing...)
attaches = append(attaches, creating...)
attaches = append(attaches, updating...)

return attaches, nil
}

func CompileAttachmentLinks(markdown []byte, attaches []Attachment) []byte {
links := map[string]string{}
names := []string{}

for _, attach := range attaches {
uri, err := url.ParseRequestURI(attach.Link)
if err != nil {
links[attach.Name] = strings.ReplaceAll("&", "&", attach.Link)
} else {
links[attach.Name] = uri.Path +
"?" + url.QueryEscape(uri.Query().Encode())
}

names = append(names, attach.Name)
}

// sort by length so first items will have bigger length
// it's helpful for replacing in case of following names
// attachments/a.jpg
// attachments/a.jpg.jpg
// so we replace longer and then shorter
sort.SliceStable(names, func(i, j int) bool {
return len(names[i]) > len(names[j])
})

for _, name := range names {
from := `attachment://` + name
to := links[name]

log.Debugf(nil, "replacing: %q -> %q", from, to)

markdown = bytes.ReplaceAll(
markdown,
[]byte(from),
[]byte(to),
)
}

return markdown
}

func getChecksum(filename string) (string, error) {
Expand Down
14 changes: 12 additions & 2 deletions pkg/mark/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func (code MacroCode) Render() string {
// compileMarkdown will replace tags like <ac:rich-tech-body> with escaped
// equivalent, because blackfriday markdown parser replaces that tags with
// <a href="ac:rich-text-body">ac:rich-text-body</a> for whatever reason.
func CompileMarkdown(markdown []byte) []byte {
func CompileMarkdown(
markdown []byte,
) []byte {
log.Tracef(nil, "rendering markdown:\n%s", string(markdown))

colon := regexp.MustCompile(`---BLACKFRIDAY-COLON---`)

tags := regexp.MustCompile(`<(/?\S+):(\S+)>`)
Expand Down Expand Up @@ -68,15 +72,21 @@ func CompileMarkdown(markdown []byte) []byte {
blackfriday.EXTENSION_TABLES |
blackfriday.EXTENSION_FENCED_CODE |
blackfriday.EXTENSION_AUTOLINK |
blackfriday.EXTENSION_LAX_HTML_BLOCKS |
blackfriday.EXTENSION_STRIKETHROUGH |
blackfriday.EXTENSION_SPACE_HEADERS |
blackfriday.EXTENSION_HEADER_IDS |
blackfriday.EXTENSION_AUTO_HEADER_IDS |
blackfriday.EXTENSION_TITLEBLOCK |
blackfriday.EXTENSION_BACKSLASH_LINE_BREAK |
blackfriday.EXTENSION_DEFINITION_LISTS,
blackfriday.EXTENSION_DEFINITION_LISTS |
blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK,
},
)

html = colon.ReplaceAll(html, []byte(`:`))

log.Tracef(nil, "rendered markdown to html:\n%s", string(html))

return html
}

0 comments on commit 51f28f8

Please sign in to comment.