Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
jeppefrandsen committed Mar 17, 2017
1 parent 8ddcace commit e567ec9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# gravizool
Gravizo tool

Small [gravizo.com](http://gravizo.com) tool for encoding and decoding GitHub Markdown files based on the new [GitHub Flavored Markdown Spec](https://github.github.com/gfm/#link-destination).
64 changes: 64 additions & 0 deletions gravizool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"os"
"fmt"
"flag"
"io/ioutil"
"strings"
)

const gravizoBegin string = "(http://g.gravizo.com/svg?"
const gravizoEnd string = "enduml)"

var encoder = strings.NewReplacer(";", "%3B", " ", "%20", "\n", "%0A", "@", "%40",
"(", "%28", ")", "%29", "*", "%2A", "\\", "%5C")
var decoder = strings.NewReplacer("%3B", ";", "%20", " ", "%0A", "\n", "%40", "@",
"%2A", "*", "%5C", "\\")

func check(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func convert(filename string, replacer *strings.Replacer, backup bool) {
buffer, err := ioutil.ReadFile(filename)
check(err)

if backup {
err = ioutil.WriteFile(fmt.Sprint(filename + ".bak"), buffer, 0644)
check(err)
}

text := string(buffer)

for _, slice := range strings.Split(text, gravizoBegin) {
if strings.Contains(slice, gravizoEnd) {
subSlice := strings.Split(slice, gravizoEnd)
if len(subSlice) > 0 {
gravizoText := subSlice[0]
convertedText := replacer.Replace(gravizoText)
text = strings.Replace(text, gravizoText, convertedText, -1)
}
}
}

err = ioutil.WriteFile(filename, []byte(text), 0644)
check(err)
}

func main() {
encode := flag.String("e", "", "Encode the given GitHub Markdown file")
decode := flag.String("d", "", "Decode the given GitHub Markdown file")
backup := flag.Bool("b", true, "Backup GitHub Markdown file before encode/decode")

flag.Parse()

if len(*encode) > 0 {
convert(*encode, encoder, *backup)
} else if len(*decode) > 0 {
convert(*decode, decoder, *backup)
}
}

0 comments on commit e567ec9

Please # to comment.