Skip to content

Commit ee683dc

Browse files
committed
added md2pic cli tools
1 parent cb45c40 commit ee683dc

File tree

9 files changed

+247
-0
lines changed

9 files changed

+247
-0
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ heroku-build-and-deploy:
1212
heroku container:release web
1313
heroku-stats:
1414
heroku ps -a pikchr-render-server
15+
16+
md2pi:
17+
go run ./cmd/md2pikchrs/main.go -in ./_tmp/*.md -out ./_out

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ You can use a shared server for rendering (set by default in settings in the vsc
2020

2121
If you have questions, comments, or suggestions, please visit the [GitHub Discussion board](https://github.com/gebv/pikchr/discussions).
2222

23+
**CLI tools for render SVG from your md file with code blocks with source code of diagram**. [Follow link](./cmd/md2pikchrs/README.md) for details
24+
2325

2426
# What is pikchr?
2527

_out/fil1.svg

Lines changed: 21 additions & 0 deletions
Loading

_out/foo_bar.svg

Lines changed: 21 additions & 0 deletions
Loading

_tmp/demo.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
```pikchr fil1.svg
2+
arrow right 200% "Markdown" "Source"
3+
box rad 10px "Markdown" "Formatter" "(markdown.c)" fit
4+
arrow right 200% "HTML+SVG" "Output"
5+
arrow <-> down from last box.s
6+
box same "Pikchr" "Formatter" "(pikchr.c)" fit
7+
```
8+
9+
```pikchr foo bar
10+
arrow right 200% "Markdown" "Source"
11+
box rad 10px "Markdown" "Formatter" "(markdown.c)" fit
12+
arrow right 200% "HTML+SVG" "Output"
13+
arrow <-> down from last box.s
14+
box same "Pikchr" "Formatter" "(pikchr.c)" fit
15+
```
16+
17+
```pikchr foo bar.svg
18+
arrow right 200% "Markdown" "Source"
19+
box rad 10px "Markdown" "Formatter" "(markdown.c)" fit
20+
arrow right 200% "HTML+SVG" "Output"
21+
arrow <-> down from last box.s
22+
box same "Pikchr" "Formatter" "(pikchr.c)" fit
23+
```
24+
25+
```pikchr foo_bar.svg
26+
arrow right 200% "Markdown" "Source"
27+
box rad 10px "Markdown" "Formatter" "(markdown.c)" fit
28+
arrow right 200% "HTML+SVG" "Output"
29+
arrow <-> down from last box.s
30+
box same "Pikchr" "Formatter" "(pikchr.c)" fit
31+
```

_tmp/some.file

Whitespace-only changes.

cmd/md2pikchrs/README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# MD to pic
2+
3+
cli tools to integrate into your development process.
4+
5+
How it works? `md2pikchrs` parse your markdown files and generated SVG files for each found code block with magic text. What is `magic text`? Follows example below
6+
7+
It is your markdown file
8+
9+
<pre class="language-text">
10+
# Some header
11+
12+
some text
13+
14+
more text...
15+
16+
```json
17+
{"foo":"bar"}
18+
```
19+
20+
```bash
21+
echo "some scripts"
22+
```
23+
24+
end more more code blocks
25+
26+
but if the block language matters __pikchr__ and after it there will be more text
27+
28+
```pikchr some text
29+
arrow right 200% "Markdown" "Source"
30+
box rad 10px "Markdown" "Formatter" "(markdown.c)" fit
31+
arrow right 200% "HTML+SVG" "Output"
32+
arrow <- down from last box.s
33+
box same "Pikchr" "Formatter" "(pikchr.c)" fit
34+
```
35+
36+
will be generated file `some_text.svg` with SVG diagram
37+
</pre>
38+
39+
# How to use
40+
41+
NOTE: in the code block, the first line with the language name is called `string info`. First word it is language name.
42+
43+
- create code block with language `pikchr`
44+
- after the language name, everything that will be specified will be the name of the generated file (before line break).
45+
46+
Nothing will be generated - not specified file name
47+
<pre>
48+
```pikchr
49+
</pre>
50+
51+
Nothing will be generated - must be `pikchr` language name
52+
<pre>
53+
```json
54+
</pre>
55+
56+
Will be generated svg file with name `foo_bar.svg`
57+
<pre>
58+
```pikchr foo bar
59+
</pre>
60+
61+
... and the some name `foo_bar.svg`
62+
<pre>
63+
```pikchr foo_bar.svg
64+
</pre>
65+
66+
... and the same name `foo_bar.svg`
67+
<pre>
68+
```pikchr foo bar.svg
69+
</pre>
70+
71+
72+
```bash
73+
md2pikchrs
74+
--out=./out/dir/with/gens/svg/fiels
75+
--in=./*.md
76+
```
77+
78+
NOTE: Files with the same name are overwritten
79+
80+
81+
82+
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+

cmd/md2pikchrs/main.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"io/ioutil"
6+
"log"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/gebv/pikchr"
12+
"github.com/gebv/pikchr/markdown"
13+
)
14+
15+
var fOutDir = flag.String("out", "./out", "Sets dir for exported diagram files.")
16+
var fInFile = flag.String("in", "./*.md", "Input markdown file.")
17+
18+
func main() {
19+
flag.Parse()
20+
validFlags()
21+
filepath.Walk(*fInFile, func(path string, info os.FileInfo, err error) error {
22+
if info.IsDir() {
23+
log.Println("Skipped dir:", path)
24+
return nil
25+
}
26+
fileDat, err := ioutil.ReadFile(path)
27+
if err != nil {
28+
log.Println("Failed open and read file:", path, err)
29+
return nil
30+
}
31+
md, err := markdown.Parse(string(fileDat))
32+
if err != nil {
33+
log.Println("Failed parse md file:", path, err)
34+
return nil
35+
}
36+
interestedBlocks := []markdown.MarkdownCodeBlock{}
37+
log.Println(path, "total", len(md.CodeBlocks()), "code blocks")
38+
for _, block := range md.CodeBlocks() {
39+
if block.Language() == "pikchr" && block.StringInfoAfterLanguageName() != "" {
40+
interestedBlocks = append(interestedBlocks, block)
41+
}
42+
}
43+
log.Println(path, len(interestedBlocks), "interesting code blocks")
44+
for _, block := range interestedBlocks {
45+
fileName := strings.ReplaceAll(strings.TrimSpace(strings.ToLower(block.StringInfoAfterLanguageName())), " ", "_")
46+
log.Println(path, "\t", fileName, "rendering...")
47+
renderRes, ok := pikchr.Render(block.Content().Raw())
48+
if !ok {
49+
log.Println(path, "\t", fileName, "failed render:", renderRes.Data)
50+
continue
51+
}
52+
if !strings.HasSuffix(fileName, ".svg") {
53+
fileName += ".svg"
54+
}
55+
if err := ioutil.WriteFile(filepath.Join(*fOutDir, fileName), []byte(renderRes.Data), 0644); err != nil {
56+
log.Println(path, "\t", fileName, "failed write the generated file:", err)
57+
}
58+
log.Println(path, "\t", fileName, "- OK")
59+
}
60+
return nil
61+
})
62+
}
63+
64+
func validFlags() {
65+
if fOutDir == nil || *fOutDir == "" {
66+
log.Println("Please sets the out dir.")
67+
os.Exit(1)
68+
}
69+
if fInFile == nil || *fInFile == "" {
70+
log.Println("Please sets in file\\files.")
71+
os.Exit(1)
72+
}
73+
}

markdown/markdown.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ type MarkdownCodeBlock struct {
2828
content *syntax.CodeBlock
2929
}
3030

31+
func (b MarkdownCodeBlock) SyntaxBlock() *syntax.CodeBlock {
32+
return b.content
33+
}
34+
3135
func (b MarkdownCodeBlock) Language() string {
3236
// NOTE: shuld be one
3337
stringInfo := b.StringInfo().Content()[0]

0 commit comments

Comments
 (0)