-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New release that parse freemind and xmind format
- Loading branch information
Showing
5 changed files
with
321 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package freemind | ||
|
||
import ( | ||
"bytes" | ||
"encoding/xml" | ||
"fmt" | ||
"io/ioutil" | ||
"os/exec" | ||
"regexp" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
var PARTS = make([]string, 0) | ||
|
||
type Map struct { | ||
Node Node | ||
} | ||
|
||
type Node struct { | ||
XMLName xml.Name `xml:"node"` | ||
Text string `xml:"TEXT,attr"` | ||
Content string `xml:"richcontent"` | ||
Nodes []Node `xml:"node"` | ||
} | ||
|
||
func write(n *Node, level int) { | ||
|
||
PARTS = append(PARTS, fmt.Sprintf("%s %s", strings.Repeat("#", level+1), n.Text)) | ||
if n.Content != "" { | ||
cmd := exec.Command("/usr/bin/pandoc", "-f", "html", "-t", "markdown") | ||
|
||
stdin, err := cmd.StdinPipe() | ||
if err != nil { | ||
panic(err) | ||
} | ||
stdout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := cmd.Start(); err != nil { | ||
panic(err) | ||
} | ||
|
||
stdin.Write([]byte(n.Content)) | ||
stdin.Close() | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
o, err := ioutil.ReadAll(stdout) | ||
if err != nil { | ||
panic(err) | ||
} | ||
PARTS = append(PARTS, string(o)) | ||
} | ||
|
||
if len(n.Nodes) > 0 { | ||
for _, n := range n.Nodes { | ||
write(&n, level+1) | ||
} | ||
} | ||
|
||
} | ||
|
||
func Open(filename, tpl string) { | ||
|
||
content, _ := ioutil.ReadFile(filename) | ||
|
||
re := regexp.MustCompile(`<richcontent (.*)>`) | ||
|
||
s := re.ReplaceAllString(string(content), "<richcontent><![CDATA[") | ||
s = strings.Replace(s, "</richcontent>", "]]></richcontent>", -1) | ||
content = []byte(s) | ||
|
||
n := Map{} | ||
if err := xml.Unmarshal(content, &n); err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
title := n.Node.Text | ||
|
||
for _, n := range n.Node.Nodes { | ||
write(&n, 0) | ||
} | ||
|
||
t, _ := template.New("doc").Parse(tpl) | ||
var b []byte | ||
buff := bytes.NewBuffer(b) | ||
t.Execute(buff, map[string]string{ | ||
"Title": title, | ||
"Content": strings.Join(PARTS, "\n\n"), | ||
}) | ||
fmt.Println(buff) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package vym | ||
|
||
import ( | ||
"archive/zip" | ||
"bytes" | ||
"encoding/xml" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
// PARTS contains document lines. | ||
var PARTS = make([]string, 0) | ||
|
||
type VymMap struct { | ||
MapCenter MapCenter `xml:"mapcenter"` | ||
} | ||
|
||
type MapCenter struct { | ||
XMLName xml.Name `xml:"mapcenter"` | ||
Heading string `xml:"heading"` | ||
Branch []Branch `xml:"branch"` | ||
} | ||
|
||
type Branch struct { | ||
XMLName xml.Name `xml:"branch"` | ||
Heading string `xml:"heading"` | ||
VymNote string `xml:"vymnote"` | ||
|
||
Branches []Branch `xml:"branch"` | ||
} | ||
|
||
// build XML tree and return *VymMap. | ||
func xmlBuildStruct(r io.ReadCloser) *VymMap { | ||
|
||
defer r.Close() | ||
c, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
panic(err) | ||
} | ||
x := VymMap{} | ||
xml.Unmarshal(c, &x) | ||
return &x | ||
|
||
} | ||
|
||
// Parse branches. | ||
func doParts(b *Branch, level int) { | ||
|
||
heading := b.Heading | ||
PARTS = append(PARTS, fmt.Sprintf("%s %s", strings.Repeat("#", level+1), heading)) | ||
PARTS = append(PARTS, b.VymNote) | ||
|
||
// parse children branches | ||
if len(b.Branches) > 0 { | ||
for _, b := range b.Branches { | ||
doParts(&b, level+1) | ||
} | ||
} | ||
|
||
} | ||
|
||
// parse the VymMap tree to build markdown. | ||
func parseVymTree(v *VymMap, tpl string) { | ||
|
||
// parse each mapcenter branches | ||
for _, b := range v.MapCenter.Branch { | ||
doParts(&b, 0) | ||
} | ||
|
||
t, _ := template.New("doc").Parse(tpl) | ||
var b []byte | ||
buff := bytes.NewBuffer(b) | ||
|
||
err := t.Execute(buff, map[string]string{ | ||
"Title": v.MapCenter.Heading, | ||
"Content": strings.Join(PARTS, "\n\n"), | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(buff) | ||
|
||
} | ||
|
||
func Open(filename string, tpl string) { | ||
// try to open file | ||
r, err := zip.OpenReader(filename) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer r.Close() | ||
|
||
for _, f := range r.File { | ||
if f.Name[len(f.Name)-4:] != ".xml" { | ||
continue | ||
} | ||
rc, err := f.Open() | ||
if err != nil { | ||
panic(err) | ||
} | ||
x := xmlBuildStruct(rc) | ||
parseVymTree(x, tpl) | ||
break | ||
} | ||
|
||
} |
Oops, something went wrong.