-
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.
- Loading branch information
1 parent
8ddcace
commit e567ec9
Showing
2 changed files
with
66 additions
and
1 deletion.
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
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). |
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,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) | ||
} | ||
} |