tree_html 0.1.8 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38b6feffd5a7b44acca5b51ac501b4de6a714d5eda38700d867b1d7a345b145b
4
- data.tar.gz: 1efb12cbe56fe1c97b658e086f7f639d82ad2190198b6868262b341964ab3fad
3
+ metadata.gz: 1c5567586e5ba48d99e6bfea031f5c43795b6c78166af1a386405f89fde58ac2
4
+ data.tar.gz: 0653e2bf6e13d584a92781a3285c882f5a610fa598a21139c88aa2cff8c8d4b7
5
5
  SHA512:
6
- metadata.gz: 281f428f38623f6b4ba961348073a1f4e9fe1a9e7f2204a857efe6950974c15a854ac2426de0b471b1d843e55a9d33fadda75302e7c8b9fceecbe399aab34769
7
- data.tar.gz: 2279a74ba2795b54a6347edbd51b95b355a45645777020135a74b84979d7eaff05a1943537554cdcf9f9accdc39e82e9aca81439a0b233de933039d780f12f5e
6
+ metadata.gz: d405b3211570e877137fe534bec36f059784ba11e7522ab09d7018f3abf4495912fc9a55f069d4fd48bb3cbf2361349ea5ab8c38e17b891afb16a35534e86f20
7
+ data.tar.gz: 4db190bfc3b7621d805c558fc10d497da71f957c322e1ec6ac4288584a1345f88cc1d2a258049c883eba8f9b9b32a4fc09efd9b8d8c8f06ae60d2918e5abb1c7
data/README.md CHANGED
@@ -20,13 +20,23 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- In Node class, `include TreeHtml`, then implement `label_for_tree_html` and `children_for_tree_html`, then call `tree_html` on node object to get ul/li fragment, or `tree_html_full` to get a html file with pre-defined style.
23
+ In Node class, `include TreeHtml`.
24
+
25
+ Then implement `label_for_tree_html` and `children_for_tree_html` for Branch class, and `label_for_tree_html` for Leaf class.
26
+
27
+ Finally, call `tree_html` on node object to get ul/li fragment, or `tree_html_full` to get a html file with pre-defined style.
24
28
 
25
29
  By Default, any object can be leaf, and renders as `to_s`.
26
30
 
27
31
  You may overwrite `data_for_tree_html`, `head_js_for_tree_html`, `body_js_for_tree_html`, `css_for_tree_html` to specify your own style.
28
32
 
29
- In generated html, hover a branch and press `f`/`u` to fold/unfold it's children, press `p`/`n` to jump to it's previous/next sibling branch. You may change these function keys in `body_js_for_tree_html`.
33
+ In generated html, hover a branch and
34
+
35
+ - press `f`/`u` to fold/unfold it's children
36
+ - press `p`/`n` to jump to it's previous/next sibling branch
37
+ - press `a` to print ascii tree in console
38
+
39
+ You may change these function keys in `body_js_for_tree_html`.
30
40
 
31
41
  Or checkout [test/tree_html_test.rb](https://github.com/turnon/tree_html/blob/master/test/tree_html_test.rb) to see how to use.
32
42
 
@@ -76,7 +76,50 @@ var TreeHtml = (function(){
76
76
  twinkle(li.querySelector('a'), 6);
77
77
  }
78
78
 
79
+
80
+ function ascii(li) {
81
+ let lines = []
82
+ function add_line(prefix, text) {
83
+ if (lines.length) lines.push('\n')
84
+ prefix = replace_branchs(prefix)
85
+ lines = lines.concat(prefix)
86
+ lines.push(' ')
87
+ lines.push(text)
88
+ }
89
+
90
+ function replace_branchs(array){
91
+ if (array.length === 0) return [];
92
+ let arr = array.map(ele => { return (ele === ' ├─' || ele === ' │ ') && ' │ ' || ' ' })
93
+ arr[array.length - 1] = array[array.length - 1]
94
+ return arr
95
+ }
96
+
97
+ function walk(li, prefix) {
98
+ let a = li.querySelector('a')
99
+ let checkbox = a.previousSibling.previousSibling
100
+ let fold = checkbox && (checkbox.checked && '+' || '-') || ' '
101
+ let text = a.innerText
102
+ add_line(prefix, fold + ' ' + text)
103
+
104
+ if (!checkbox || checkbox.checked) return
105
+ let ul = li.querySelector('ul')
106
+ if (ul === null) return
107
+ let i = 1
108
+ ul.childNodes.forEach(child => {
109
+ let branch = ul.childNodes.length === i ? ' └─' : ' ├─'
110
+ prefix.push(branch)
111
+ walk(child, prefix)
112
+ prefix.pop()
113
+ i = i + 1
114
+ })
115
+ }
116
+
117
+ walk(li, [])
118
+ console.log(lines.join(''))
119
+ }
120
+
79
121
  var actions = {
122
+ ascii: ascii,
80
123
  fold: function(node) {
81
124
  check_children(node, true);
82
125
  },
@@ -93,7 +136,7 @@ var TreeHtml = (function(){
93
136
  }
94
137
  };
95
138
 
96
- var key_cmds = {f: 'fold', u: 'unfold', p: 'prev', n: 'next'};
139
+ var key_cmds = {f: 'fold', u: 'unfold', p: 'prev', n: 'next', a: 'ascii'};
97
140
 
98
141
  var resp_keypress = [function(e) {
99
142
  e = e || window.event;
@@ -1,3 +1,3 @@
1
1
  module TreeHtml
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.10"
3
3
  end
data/lib/tree_html.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "tree_html/version"
2
+ require "cgi"
2
3
 
3
4
  module TreeHtml
4
5
 
@@ -11,7 +12,11 @@ module TreeHtml
11
12
 
12
13
  class ::Object
13
14
  def li_for_tree_html
14
- "<li>#{NO_CHECKBOX}<a>#{to_s}</a></li>"
15
+ "<li>#{NO_CHECKBOX}<a>#{label_for_tree_html}</a></li>"
16
+ end
17
+
18
+ def label_for_tree_html
19
+ CGI::escapeHTML(to_s)
15
20
  end
16
21
  end
17
22
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tree_html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - ken
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-21 00:00:00.000000000 Z
11
+ date: 2022-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler