Skip to content

Commit 8fb1f1c

Browse files
committed
tree: accept native tree command output #1159
1 parent b73c688 commit 8fb1f1c

File tree

5 files changed

+157
-35
lines changed

5 files changed

+157
-35
lines changed

docs/content/introduction/releasenotes/8/3.en.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ weight = -3
2222
To activate it set `urlExternalCheck=true` in the your `hugo.toml` `params` section.
2323

2424
The name of option `ignoreerror` was changed to `urlIgnoreError` in that matter. You can leave your config unchanged but will see warnings if you are using the deprecated name.
25+
26+
- {{% badge style="new" %}}New{{% /badge %}} The [`tree` shortcode] learned to also [format output of the `tree` command](shortcodes/tree/#tree-command-output) of your favorite operating system.

docs/content/shortcodes/tree/_index.en.md

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
+++
22
categories = ['howto', 'reference']
3-
description = 'Display a list as a tree'
3+
description = 'Display text as a tree'
44
title = 'Tree'
55
+++
66

7-
The `tree` shortcode displays a list as a tree with configurable icons and colors.
7+
The `tree` shortcode displays text as a tree with configurable icons and colors.
88

99
````tree
1010
- home | folder
@@ -61,7 +61,7 @@ Markdown codefence syntax is widely available in other Markdown parsers like Git
6161

6262
| Name | Default | Notes |
6363
|-----------------------|------------------|-------------|
64-
| _**<content>**_ | _<empty>_ | Your list as Markdown. |
64+
| _**<content>**_ | _<empty>_ | Your list as Markdown or from your favorite `tree` commandline tool. |
6565

6666
### Item Syntax
6767

@@ -85,46 +85,76 @@ The **NAME** can be followed by an optional pipe (`|`) to define an **ICON** and
8585

8686
## Examples
8787

88-
### Bigger example
88+
### `tree` Command Output
89+
90+
Use the `tree` command of your favorite operating system and dump it right into the shortcode. Note, that the directory marker (here `C:.`) is removed if present.
91+
92+
````md
93+
```tree
94+
C:.
95+
│ featured.png
96+
│ index.en.md
97+
│ index.pir.md
98+
│ MaybeTreasure.en.txt
99+
│ MaybeTreasure.pir.txt
100+
│ NoTreasure.en.txt
101+
│ Treasure.pir.txt
102+
103+
└───subdir
104+
hugo.png
105+
```
106+
````
107+
108+
````tree
109+
C:.
110+
│ featured.png
111+
│ index.en.md
112+
│ index.pir.md
113+
│ MaybeTreasure.en.txt
114+
│ MaybeTreasure.pir.txt
115+
│ NoTreasure.en.txt
116+
│ Treasure.pir.txt
117+
118+
└───subdir
119+
hugo.png
120+
````
121+
122+
### Markdown List with Styled Items
89123

90124
Every possible combination
91125

92126
````md
93127
```tree
94-
- [just name](http://example.com) | folder
95-
- Documents
96-
- My Documents
128+
- just names
129+
- Document
130+
- My Document
131+
- [My linked Document](http://example.com)
97132
- simple icons | folder
98-
- Documents | folder
99-
- My Documents | folder
100-
- not so simple icons | file-alt | secondary
101-
- a.png | file-alt
102-
- a with border.png | file-alt
103-
- mindblowing icons | fa-fw fab fa-markdown | accent
104-
- text.md | fa-fw fab fa-markdown
105-
- alternative text.md | fa-fw fab fa-markdown
106-
- and now with color | fa-fw fab fa-php
133+
- Document | file
134+
- My Document | file
135+
- mindblowing icons
136+
- index.md | fa-fw fab fa-markdown
137+
- index.html | fa-fw fab fa-html5
138+
- and now with color
107139
- script.php | fa-fw fab fa-markdown | purple
108140
- alt script.php | fa-fw fab fa-markdown | #888cc4
109141
- magic.php | fa-fw fab fa-markdown | magic
110142
```
111143
````
112144

113145
````tree
114-
- [just name](http://example.com) | folder
115-
- Documents
116-
- My Documents
146+
- just names
147+
- Document
148+
- My Document
149+
- [My linked Document](http://example.com)
117150
- simple icons | folder
118-
- Documents | folder
119-
- My Documents | folder
120-
- not so simple icons | file-alt | secondary
121-
- a.png | file-alt
122-
- a with border.png | file-alt
123-
- mindblowing icons | fa-fw fab fa-markdown | accent
124-
- text.md | fa-fw fab fa-markdown
125-
- alternative text.md | fa-fw fab fa-markdown
126-
- and now with color | fa-fw fab fa-php
127-
- script.php | fa-fw fab fa-php | purple
128-
- alt script.php | fa-fw fab fa-php | #888cc4
129-
- magic.php | fa-fw fab fa-php | magic
151+
- Document | file
152+
- My Document | file
153+
- mindblowing icons
154+
- index.md | fa-fw fab fa-markdown
155+
- index.html | fa-fw fab fa-html5
156+
- and now with color
157+
- script.php | fa-fw fab fa-markdown | purple
158+
- alt script.php | fa-fw fab fa-markdown | #888cc4
159+
- magic.php | fa-fw fab fa-markdown | magic
130160
````
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
+++
22
categories = ['howto', 'reference']
3-
description = 'Display a list as a tree'
3+
description = 'Display text as a tree'
44
title = 'Tree'
55
+++
66
{{< piratify >}}

layouts/partials/shortcodes/tree.html

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
{{ $content := .content | .page.RenderString }}
1+
{{- $content := .content }}
2+
{{- $content = replaceRE `^\s*\r?\n` "" $content }}
3+
{{- $content = partial "inline/tree2markdown" $content }}
4+
{{- $content = $content | .page.RenderString -}}
25
<div class="list-tree">
36
{{- $matches := findRESubmatch `<li>([\s\S]*?)(</li|<ul)` $content }}
47
{{- range $matches }}
@@ -35,4 +38,91 @@
3538
{{- end }}
3639
{{- end }}
3740
{{- $content | safeHTML -}}
38-
</div>
41+
</div>
42+
43+
{{- define "partials/inline/tree2markdown" }}
44+
{{- /* this parses native `tree` output into a Markdown list */}}
45+
{{- $content := . }}
46+
{{- $indentChars := slice " " "\t" }}
47+
{{- $treeColChars := slice "│" "├" "└" "─" }}
48+
{{- $indentPattern := "" }}
49+
{{- $treeColPattern := "" }}
50+
{{- $treeColPatternLen := 0 }}
51+
52+
{{- $firstLine := true }}
53+
{{- $lines := split $content "\n" }}
54+
{{- range $lineIndex, $line := $lines }}
55+
{{- /* the first text line and all empty lines are skipped */}}
56+
{{- $skipLine := false }}
57+
{{- if not (trim $line " \t\r") }}
58+
{{- $skipLine = true }}
59+
{{- else if $firstLine }}
60+
{{- $firstLine = false }}
61+
{{- $skipLine = true }}
62+
{{- range split $line "" }}
63+
{{- if in $treeColChars . }}
64+
{{- $skipLine = false }}
65+
{{- break }}
66+
{{- end }}
67+
{{- end }}
68+
{{- end }}
69+
{{- if $skipLine }}
70+
{{- continue }}
71+
{{- end }}
72+
73+
{{- if not $treeColPattern }}
74+
{{- /* for the first non-empty line we try to find out if it is a tree line and set prefix column pattern according to its column width */}}
75+
{{- range $char := (split $line "") }}
76+
{{- if and (not $treeColPattern) (in $indentChars $char) }}
77+
{{- $indentPattern = print $indentPattern "." }}
78+
{{- else if in $treeColChars $char }}
79+
{{- if $treeColPattern }}
80+
{{- $treeColPattern = print $treeColPattern "[─ ]" }}
81+
{{- $treeColPatternLen = add $treeColPatternLen 1 }}
82+
{{- else }}
83+
{{- $treeColPattern = print $treeColPattern "[│├└ ]" }}
84+
{{- $treeColPatternLen = add $treeColPatternLen 1 }}
85+
{{- end }}
86+
{{- else if eq " " $char }}
87+
{{- $treeColPattern = print $treeColPattern "[─ ]" }}
88+
{{- $treeColPatternLen = add $treeColPatternLen 1 }}
89+
{{- else }}
90+
{{- break }}
91+
{{- end }}
92+
{{- end }}
93+
{{- if not $treeColPattern }}
94+
{{- /* it is not a tree line so break processing at all and leave content unchanged */}}
95+
{{- break }}
96+
{{- end }}
97+
{{- /* it is a tree line so we reset the content and fill it by parsing next */}}
98+
{{- $content = "" }}
99+
{{- end }}
100+
101+
{{- $linePattern := print `^` $indentPattern `((?:` $treeColPattern `)*)([^` (delimit $treeColChars "") `].*)` }}
102+
{{- range findRESubmatch $linePattern $line }}
103+
{{- $newline := "" }}
104+
{{- $rest := (index . 2) }}
105+
{{- $treePrefix := (index . 1) }}
106+
{{- $treePrefixLen := 0 }}
107+
{{- range (split $treePrefix "") }}
108+
{{- /* treeChars may contain Unicode chars which len messes up as it only counts bytes */}}
109+
{{- $treePrefixLen = add $treePrefixLen 1 }}
110+
{{- end }}
111+
{{- $treeLevel := div $treePrefixLen $treeColPatternLen }}
112+
{{- range seq $treeLevel }}
113+
{{- if eq . $treeLevel }}
114+
{{- /* last column needs its dash */}}
115+
{{- $newline = print $newline "- " }}
116+
{{- else }}
117+
{{- $newline = print $newline " " }}
118+
{{- end }}
119+
{{- end }}
120+
{{- /* have seen some weird Windows output that puts in empty lines in the tree, so only write if we have prefix AND rest */}}
121+
{{- $rest = trim $rest " \t\r" }}
122+
{{- if and $newline $rest }}
123+
{{- $content = print $content $newline $rest "\n" }}
124+
{{- end }}
125+
{{- end }}
126+
{{- end }}
127+
{{- return $content }}
128+
{{- end }}

layouts/partials/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8.2.0+1848de48ae27a0fe95489bb68a8cd0fd21391226
1+
8.2.0+b73c688da2649b4d509f25ec53047c05835b3f12

0 commit comments

Comments
 (0)