tty-markdown 0.3.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +64 -1
- data/README.md +121 -59
- data/lib/tty-markdown.rb +1 -1
- data/lib/tty/markdown.rb +127 -72
- data/lib/tty/markdown/converter.rb +821 -0
- data/lib/tty/markdown/kramdown_ext.rb +23 -0
- data/lib/tty/markdown/syntax_highlighter.rb +20 -16
- data/lib/tty/markdown/version.rb +3 -1
- metadata +44 -66
- data/.gitignore +0 -12
- data/.rspec +0 -3
- data/.travis.yml +0 -23
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -16
- data/Rakefile +0 -8
- data/appveyor.yml +0 -23
- data/assets/headers.png +0 -0
- data/assets/hr.png +0 -0
- data/assets/link.png +0 -0
- data/assets/list.png +0 -0
- data/assets/quote.png +0 -0
- data/assets/syntax_highlight.png +0 -0
- data/assets/table.png +0 -0
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/examples/example.md +0 -49
- data/examples/man.md +0 -17
- data/examples/man.rb +0 -6
- data/examples/marked.rb +0 -6
- data/lib/tty/markdown/parser.rb +0 -467
- data/tasks/console.rake +0 -11
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
- data/tty-markdown.gemspec +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fa4ae8d84f0debb66b9d49522b36c335062c9932072b61eda061fed1408236b6
|
4
|
+
data.tar.gz: 2d79a3688cb49f9a5b1f5c1d11c9f4b8d9654990472219fe412b9039f8dc6373
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3af37ef74648802261e1768acf170134cc0d523a4af52f4a666e4aa52202b45da3c3e749e4abca8976b79b0b3e0fb5ac540ee2289cc37d296facf07a644bf1e1
|
7
|
+
data.tar.gz: b413d03ef5fb97c1f808154d4ddc8386763e0c4229ed7e0d20f515988f9c29b9210c7d707257d044ef3f95d88af46c6de825811eb5d4d9a4a5e9aa1a83f6fc68
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,63 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.7.0] - 2020-09-03
|
4
|
+
|
5
|
+
### Added
|
6
|
+
* Add converter for br element by @krage
|
7
|
+
* Add configuration of symbols by @krage
|
8
|
+
* Add definition list support
|
9
|
+
* Add table footer formatting
|
10
|
+
* Add formatting of image source location
|
11
|
+
* Add footnotes support
|
12
|
+
* Add XML comments support
|
13
|
+
* Add HTML div/i/em/b/strong/img/a element support
|
14
|
+
* Add color configuration setting
|
15
|
+
|
16
|
+
### Changed
|
17
|
+
* Change #new to accept configuration options as keywords
|
18
|
+
* Display links with both label and optional title by @krage
|
19
|
+
* Display link without the label when label content is same as link target @krage
|
20
|
+
* Change hr formatting to be always full width
|
21
|
+
* Differentiate between span and block math elements
|
22
|
+
* Display abbreviation with its definition
|
23
|
+
* Improve performance by calculating table column widths and row heights only once
|
24
|
+
* Change to remove mailto: from email links for brevity @krage
|
25
|
+
* Change to update kramdown to support versions >= 1.16 and < 3.0
|
26
|
+
* Change to update pastel, tty-color & tty-screen dependencies
|
27
|
+
* Change HTML del element formatting to strikethrough
|
28
|
+
* Rename colors setting to mode
|
29
|
+
|
30
|
+
### Fixed
|
31
|
+
* Fix break line handling by @krage
|
32
|
+
* Fix links handling by @krage
|
33
|
+
* Fix table formatting of empty cells
|
34
|
+
* Fix content wrapping to account for the current indentation
|
35
|
+
* Fix header wrapping
|
36
|
+
* Fix blockquote formatting of content with emphasised style or apostrophe
|
37
|
+
* Fix support for HTML del element
|
38
|
+
|
39
|
+
## [v0.6.0] - 2019-03-30
|
40
|
+
|
41
|
+
### Added
|
42
|
+
* Add markdown xml comments conversion
|
43
|
+
|
44
|
+
## [v0.5.1] - 2019-02-07
|
45
|
+
|
46
|
+
### Fixed
|
47
|
+
* Fix spaces around inline code quotes collapses inside list items
|
48
|
+
|
49
|
+
## [v0.5.0] - 2018-12-13
|
50
|
+
|
51
|
+
### Changed
|
52
|
+
* Change gemspec to load files directly
|
53
|
+
* Change to update rouge dependency
|
54
|
+
* Change to relax constraints on tty-screen and tty-color
|
55
|
+
|
56
|
+
## [v0.4.0] - 2018-06-20
|
57
|
+
|
58
|
+
## Fixed
|
59
|
+
* Fix multiline paragraph indentation by Brett(@suwyn)
|
60
|
+
|
3
61
|
## [v0.3.0] - 2018-03-17
|
4
62
|
|
5
63
|
### Added
|
@@ -20,7 +78,7 @@
|
|
20
78
|
* Add markdown math formula conversion
|
21
79
|
* Add markdown typogrpahic symbols conversion by Tanaka Masaki(@T-a-n-a-k-a-M-a-s-a-k-i)
|
22
80
|
* Add html entities conversion
|
23
|
-
* Add warnings about unsupported conversions for
|
81
|
+
* Add warnings about unsupported conversions for completeness
|
24
82
|
|
25
83
|
### Changed
|
26
84
|
* Change gemspec to require Ruby >= 2.0.0
|
@@ -32,6 +90,11 @@
|
|
32
90
|
|
33
91
|
* Initial implementation and release
|
34
92
|
|
93
|
+
[v0.7.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.6.0...v0.7.0
|
94
|
+
[v0.6.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.5.1...v0.6.0
|
95
|
+
[v0.5.1]: https://github.com/piotrmurach/tty-markdown/compare/v0.5.0...v0.5.1
|
96
|
+
[v0.5.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.4.0...v0.5.0
|
97
|
+
[v0.4.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.3.0...v0.4.0
|
35
98
|
[v0.3.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.2.0...v0.3.0
|
36
99
|
[v0.2.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.1.0...v0.2.0
|
37
100
|
[v0.1.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.1.0
|
data/README.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
<div align="center">
|
2
|
+
<a href="https://piotrmurach.github.io/tty" target="_blank"><img width="130" src="https://github.com/piotrmurach/tty/raw/master/images/tty.png" alt="tty logo" /></a>
|
3
|
+
</div>
|
4
|
+
|
1
5
|
# TTY::Markdown [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
|
2
6
|
|
3
7
|
[![Gem Version](https://badge.fury.io/rb/tty-markdown.svg)][gem]
|
@@ -17,7 +21,6 @@
|
|
17
21
|
|
18
22
|
> Convert a markdown document or text into a terminal friendly output.
|
19
23
|
|
20
|
-
|
21
24
|
**TTY::Markdown** provides independent markdown processing component for [TTY](https://github.com/piotrmurach/tty) toolkit.
|
22
25
|
|
23
26
|
## Installation
|
@@ -41,15 +44,21 @@ Or install it yourself as:
|
|
41
44
|
* [1. Usage](#1-usage)
|
42
45
|
* [1.1 Header](#11-header)
|
43
46
|
* [1.2 List](#12-list)
|
44
|
-
* [1.3
|
45
|
-
* [1.4
|
46
|
-
* [1.5
|
47
|
-
* [1.6
|
48
|
-
* [1.7
|
47
|
+
* [1.3 Definition List](#13-definition-list)
|
48
|
+
* [1.4 Link](#14-link)
|
49
|
+
* [1.5 Blockquote](#15-blockquote)
|
50
|
+
* [1.6 Code and Syntax Highlighting](#16-code-and-syntax-highlighting)
|
51
|
+
* [1.7 Table](#17-table)
|
52
|
+
* [1.8 Horizontal Rule](#18-horizontal-rule)
|
53
|
+
* [1.9 Footnotes](#19-footnotes)
|
49
54
|
* [2. Options](#2-options)
|
50
|
-
* [2.1 :
|
55
|
+
* [2.1 :mode](#21-mode)
|
51
56
|
* [2.2 :theme](#22-theme)
|
52
57
|
* [2.3 :width](#23-width)
|
58
|
+
* [2.4 :symbols](#24-symbols)
|
59
|
+
* [2.5 :indent](#25-indent)
|
60
|
+
* [2.6 :color](#26-color)
|
61
|
+
* [3. Command line tool](#3-command-line-tool)
|
53
62
|
|
54
63
|
## 1. Usage
|
55
64
|
|
@@ -64,12 +73,14 @@ puts parsed
|
|
64
73
|
The `parse_file` allows you to transform a markdown document into a terminal formatted output:
|
65
74
|
|
66
75
|
```ruby
|
67
|
-
parsed = TTY::Markdown.
|
76
|
+
parsed = TTY::Markdown.parse_file('example.md')
|
68
77
|
puts parsed
|
69
78
|
```
|
70
79
|
|
71
80
|
### 1.1 Header
|
72
81
|
|
82
|
+
Parsing the following markdown headers:
|
83
|
+
|
73
84
|
```markdown
|
74
85
|
TTY::Markdown
|
75
86
|
=============
|
@@ -81,15 +92,9 @@ TTY::Markdown
|
|
81
92
|
### Nested list items
|
82
93
|
```
|
83
94
|
|
84
|
-
|
85
|
-
|
86
|
-
```ruby
|
87
|
-
parsed = TTY::Markdown.parse(markdown_string)
|
88
|
-
```
|
89
|
-
|
90
|
-
`puts parsed` will output:
|
95
|
+
The terminal output looks like this:
|
91
96
|
|
92
|
-
![
|
97
|
+
![Headers example](assets/headers.png)
|
93
98
|
|
94
99
|
### 1.2 List
|
95
100
|
|
@@ -103,29 +108,42 @@ Both numbered and unordered lists are supported. Given a markdown:
|
|
103
108
|
- Item 5
|
104
109
|
```
|
105
110
|
|
106
|
-
|
111
|
+
The parsed output looks like this:
|
107
112
|
|
108
|
-
|
109
|
-
|
113
|
+
![Unordered list example](assets/list.png)
|
114
|
+
|
115
|
+
### 1.3 Definition List
|
116
|
+
|
117
|
+
Given a definition list:
|
118
|
+
|
119
|
+
```markdown
|
120
|
+
Item 1
|
121
|
+
: This is the description for Item 1
|
122
|
+
|
123
|
+
Item 2
|
124
|
+
: This is the description for Item 2
|
125
|
+
: This is another description for Item 2
|
110
126
|
```
|
111
127
|
|
112
|
-
|
128
|
+
The parsed output looks like this:
|
113
129
|
|
114
|
-
![
|
130
|
+
![Definition list example](assets/definition-list.png)
|
115
131
|
|
116
|
-
### 1.
|
132
|
+
### 1.4 Link
|
117
133
|
|
118
|
-
A
|
134
|
+
A markdown link:
|
119
135
|
|
120
136
|
```markdown
|
121
|
-
[
|
137
|
+
[An inline-style link](https://ttytoolkit.org)
|
138
|
+
|
139
|
+
[An inline-style link with title](https://ttytoolkit.org "TTY Toolkit Homepage")
|
122
140
|
```
|
123
141
|
|
124
|
-
will be rendered with
|
142
|
+
The link text will be rendered with the link next to it:
|
125
143
|
|
126
|
-
![
|
144
|
+
![Link example](assets/link.png)
|
127
145
|
|
128
|
-
### 1.
|
146
|
+
### 1.5 Blockquote
|
129
147
|
|
130
148
|
Given a markdown quote:
|
131
149
|
|
@@ -135,19 +153,15 @@ Given a markdown quote:
|
|
135
153
|
> *Oh*, you can put **Markdown** into a blockquote.
|
136
154
|
```
|
137
155
|
|
138
|
-
|
139
|
-
|
140
|
-
```ruby
|
141
|
-
parsed = TTY::Markdown.parse(markdown_string)
|
142
|
-
```
|
156
|
+
The rendered output looks like this:
|
143
157
|
|
144
|
-
|
158
|
+
![Blockquote example](assets/quote.png)
|
145
159
|
|
146
|
-
|
160
|
+
### 1.6 Code and Syntax Highlighting
|
147
161
|
|
148
|
-
|
162
|
+
The parser can highlight syntax of many programming languages.
|
149
163
|
|
150
|
-
|
164
|
+
Given a markdown codeblock with a language specification:
|
151
165
|
|
152
166
|
````markdown
|
153
167
|
```ruby
|
@@ -159,17 +173,11 @@ end
|
|
159
173
|
```
|
160
174
|
````
|
161
175
|
|
162
|
-
|
163
|
-
|
164
|
-
```ruby
|
165
|
-
parsed = TTY::Markdown.parse(code_snippet)
|
166
|
-
```
|
167
|
-
|
168
|
-
`puts parsed` will produce:
|
176
|
+
The terminal output will look like this:
|
169
177
|
|
170
|
-
![Code
|
178
|
+
![Code highlighting example](assets/codeblock.png)
|
171
179
|
|
172
|
-
### 1.
|
180
|
+
### 1.7 Table
|
173
181
|
|
174
182
|
You can transform tables which understand the markdown alignment.
|
175
183
|
|
@@ -183,17 +191,11 @@ For example, given the following table:
|
|
183
191
|
| col 3 is | right-aligned | $1 |
|
184
192
|
```
|
185
193
|
|
186
|
-
|
187
|
-
|
188
|
-
```ruby
|
189
|
-
parsed = TTY::Markdown.parse(markdown_string)
|
190
|
-
```
|
191
|
-
|
192
|
-
`puts parsed` will output:
|
194
|
+
Then the terminal output will look like this:
|
193
195
|
|
194
|
-
![
|
196
|
+
![Table example](assets/table.png)
|
195
197
|
|
196
|
-
### 1.
|
198
|
+
### 1.8 Horizontal Rule
|
197
199
|
|
198
200
|
You can specify a horizontal rule in markdown:
|
199
201
|
|
@@ -209,19 +211,38 @@ parsed = TTY::Markdown.parse(markdown_string)
|
|
209
211
|
|
210
212
|
`puts parsed` will output:
|
211
213
|
|
212
|
-
![
|
214
|
+
![Horizontal rule example](assets/hr.png)
|
215
|
+
|
216
|
+
### 1.9 Footnotes
|
217
|
+
|
218
|
+
You can create footnote references:
|
219
|
+
|
220
|
+
```markdown
|
221
|
+
It is not down on any map[^foo]; true places[^bar] never are.
|
222
|
+
|
223
|
+
[^foo]: A diagrammatic representation of an area of land or sea.
|
224
|
+
[^bar]: A particular position, point, or area in space; a location.
|
225
|
+
```
|
226
|
+
|
227
|
+
All footnotes will be displayed with a sequential number and rendered in the terminal like this:
|
228
|
+
|
229
|
+
![Footnotes example](assets/footnotes.png)
|
213
230
|
|
214
231
|
## 2. Options
|
215
232
|
|
216
|
-
### 2.1 `:
|
233
|
+
### 2.1 `:mode`
|
217
234
|
|
218
|
-
By default the `256`
|
235
|
+
By default the `256` color scheme is used to render code block elements.
|
236
|
+
|
237
|
+
You can change this by specifying maximum number of colors to be `16` ANSI colors:
|
219
238
|
|
220
239
|
```ruby
|
221
|
-
TTY::Markdown.pasre(markdown_string,
|
240
|
+
TTY::Markdown.pasre(markdown_string, mode: 16)
|
222
241
|
```
|
223
242
|
|
224
|
-
This
|
243
|
+
This feature may be handy when working in terminals with limited color support.
|
244
|
+
|
245
|
+
By default, **TTY::Markdown** detects your terminal color mode and adjusts output automatically.
|
225
246
|
|
226
247
|
### 2.2 `:theme`
|
227
248
|
|
@@ -237,6 +258,9 @@ THEME = {
|
|
237
258
|
strong: [:yellow, :bold],
|
238
259
|
table: :yellow,
|
239
260
|
quote: :yellow,
|
261
|
+
image: :bright_black,
|
262
|
+
note: :yellow,
|
263
|
+
comment: :bright_black
|
240
264
|
}
|
241
265
|
```
|
242
266
|
|
@@ -256,6 +280,44 @@ TTY::Markdown.parse(markdown_string, width: 80)
|
|
256
280
|
|
257
281
|
By default the terminal screen width is used.
|
258
282
|
|
283
|
+
### 2.4 `:symbols`
|
284
|
+
|
285
|
+
By default formatting will include various Unicode symbols. You can switch to an included ASCII set and/or override individually with the `:symbols` key:
|
286
|
+
|
287
|
+
```ruby
|
288
|
+
TTY::Markdown.parse(markdown_string, symbols: :ascii)
|
289
|
+
TTY::Markdown.parse(markdown_string, symbols: {base: :ascii})
|
290
|
+
TTY::Markdown.parse(markdown_string, symbols: {override: {bullet: "x"}})
|
291
|
+
```
|
292
|
+
|
293
|
+
### 2.5 `:indent`
|
294
|
+
|
295
|
+
By default any content apart from the main `h1` header is indented with `2` spaces. Use `:indent` to provide custom indent or no indent at all:
|
296
|
+
|
297
|
+
```ruby
|
298
|
+
TTY::Markdown.parse(markdown_string, indent: 0)
|
299
|
+
```
|
300
|
+
|
301
|
+
### 2.6 `:color`
|
302
|
+
|
303
|
+
You can control when to apply coloring to various document elements.
|
304
|
+
|
305
|
+
Valid values are `:never`, `:always` or `:auto`. By default `:auto` is used which auto detects if coloring can be applied.
|
306
|
+
|
307
|
+
For example, to always color content regardless of terminal support do:
|
308
|
+
|
309
|
+
```ruby
|
310
|
+
TTY::Markdown.parse(markdown_string, color: :always)
|
311
|
+
```
|
312
|
+
|
313
|
+
### 3. Command line tool
|
314
|
+
|
315
|
+
You can install [tty-markdown-cli](https://github.com/piotrmurach/tty-markdown-cli) to use `tty-markdown` executable in terminal:
|
316
|
+
|
317
|
+
```bash
|
318
|
+
$ tty-markdown README.md
|
319
|
+
```
|
320
|
+
|
259
321
|
## Development
|
260
322
|
|
261
323
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/tty-markdown.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "tty/markdown"
|
data/lib/tty/markdown.rb
CHANGED
@@ -1,95 +1,127 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "kramdown/document"
|
4
|
+
require "tty-color"
|
5
|
+
require "tty-screen"
|
4
6
|
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
+
require_relative "markdown/converter"
|
8
|
+
require_relative "markdown/version"
|
9
|
+
require_relative "markdown/kramdown_ext"
|
7
10
|
|
8
11
|
module TTY
|
9
12
|
module Markdown
|
10
13
|
SYMBOLS = {
|
11
|
-
arrow:
|
12
|
-
bullet:
|
13
|
-
bar:
|
14
|
-
diamond:
|
15
|
-
pipe:
|
16
|
-
line:
|
17
|
-
hellip:
|
18
|
-
laquo:
|
19
|
-
laquo_space:
|
20
|
-
raquo:
|
21
|
-
raquo_space:
|
22
|
-
ndash:
|
14
|
+
arrow: "»",
|
15
|
+
bullet: "●",
|
16
|
+
bar: "┃",
|
17
|
+
diamond: "◈",
|
18
|
+
pipe: "│",
|
19
|
+
line: "─",
|
20
|
+
hellip: "…",
|
21
|
+
laquo: "«",
|
22
|
+
laquo_space: "« ",
|
23
|
+
raquo: "»",
|
24
|
+
raquo_space: " »",
|
25
|
+
ndash: "-",
|
23
26
|
mdash: "\u2014",
|
24
|
-
lsquo:
|
25
|
-
rsquo:
|
26
|
-
ldquo:
|
27
|
-
rdquo:
|
28
|
-
top_left:
|
29
|
-
top_right:
|
30
|
-
top_center:
|
31
|
-
mid_left:
|
32
|
-
mid_right:
|
33
|
-
mid_center:
|
34
|
-
bottom_right:
|
35
|
-
bottom_left:
|
36
|
-
bottom_center:
|
27
|
+
lsquo: "‘",
|
28
|
+
rsquo: "’",
|
29
|
+
ldquo: "“",
|
30
|
+
rdquo: "”",
|
31
|
+
top_left: "┌",
|
32
|
+
top_right: "┐",
|
33
|
+
top_center: "┬",
|
34
|
+
mid_left: "├",
|
35
|
+
mid_right: "┤",
|
36
|
+
mid_center: "┼",
|
37
|
+
bottom_right: "┘",
|
38
|
+
bottom_left: "└",
|
39
|
+
bottom_center: "┴",
|
40
|
+
paren_left: "(",
|
41
|
+
paren_right: ")",
|
42
|
+
bracket_left: "[",
|
43
|
+
bracket_right: "]",
|
44
|
+
hash: "#",
|
45
|
+
delete: "\u0336"
|
37
46
|
}.freeze
|
38
47
|
|
39
|
-
|
40
|
-
arrow:
|
41
|
-
bullet:
|
42
|
-
diamond:
|
43
|
-
bar:
|
44
|
-
pipe:
|
45
|
-
line:
|
46
|
-
hellip:
|
47
|
-
laquo:
|
48
|
-
laquo_space:
|
49
|
-
raquo:
|
50
|
-
raquo_space:
|
51
|
-
ndash:
|
48
|
+
ASCII_SYMBOLS = {
|
49
|
+
arrow: "->",
|
50
|
+
bullet: "*",
|
51
|
+
diamond: "*",
|
52
|
+
bar: "│",
|
53
|
+
pipe: "|",
|
54
|
+
line: "-",
|
55
|
+
hellip: "...",
|
56
|
+
laquo: "<<",
|
57
|
+
laquo_space: "<< ",
|
58
|
+
raquo: ">>",
|
59
|
+
raquo_space: " >>",
|
60
|
+
ndash: "-",
|
52
61
|
mdash: "--",
|
53
|
-
lsquo:
|
54
|
-
rsquo:
|
55
|
-
ldquo:
|
56
|
-
rdquo:
|
57
|
-
top_left:
|
58
|
-
top_right:
|
59
|
-
top_center:
|
60
|
-
mid_left:
|
61
|
-
mid_right:
|
62
|
-
mid_center:
|
63
|
-
bottom_right:
|
64
|
-
bottom_left:
|
65
|
-
bottom_center:
|
62
|
+
lsquo: "\"",
|
63
|
+
rsquo: "\"",
|
64
|
+
ldquo: "\"",
|
65
|
+
rdquo: "\"",
|
66
|
+
top_left: "+",
|
67
|
+
top_right: "+",
|
68
|
+
top_center: "+",
|
69
|
+
mid_left: "+",
|
70
|
+
mid_right: "+",
|
71
|
+
mid_center: "+",
|
72
|
+
bottom_right: "+",
|
73
|
+
bottom_left: "+",
|
74
|
+
bottom_center: "+",
|
75
|
+
paren_left: "(",
|
76
|
+
paren_right: ")",
|
77
|
+
bracket_left: "[",
|
78
|
+
bracket_right: "]",
|
79
|
+
hash: "#",
|
80
|
+
delete: "\u0336"
|
66
81
|
}.freeze
|
67
82
|
|
68
83
|
THEME = {
|
69
84
|
em: :yellow,
|
70
|
-
header: [
|
85
|
+
header: %i[cyan bold],
|
71
86
|
hr: :yellow,
|
72
|
-
link: [
|
87
|
+
link: %i[yellow underline],
|
73
88
|
list: :yellow,
|
74
|
-
strong: [
|
89
|
+
strong: %i[yellow bold],
|
75
90
|
table: :yellow,
|
76
91
|
quote: :yellow,
|
92
|
+
image: :bright_black,
|
93
|
+
note: :yellow,
|
94
|
+
comment: :bright_black
|
77
95
|
}.freeze
|
78
96
|
|
79
97
|
# Parse a markdown string
|
80
98
|
#
|
81
|
-
# @param [Hash] options
|
82
|
-
# @option options [String] :colors
|
83
|
-
# a number of colors supported
|
84
|
-
# @option options [String] :width
|
85
|
-
#
|
86
99
|
# @param [String] source
|
87
100
|
# the source with markdown
|
101
|
+
# @param [Integer] :mode
|
102
|
+
# a number of colors supported
|
103
|
+
# @param [Integer] :indent
|
104
|
+
# the indent of the converted output
|
105
|
+
# @param [Hash<Symbol, String>] :symbols
|
106
|
+
# the symbols to use when generating output
|
107
|
+
# @param [Hash<Symbol, Symbol>] :theme
|
108
|
+
# the color names for markdown elements
|
109
|
+
# @param [Integer] :width
|
110
|
+
# the width at which to wrap content
|
111
|
+
# @param [Boolean] :color
|
112
|
+
# when to enable coloring out of always, never or auto
|
113
|
+
# @param [Hash] :doc_opts
|
114
|
+
# the markdown document parser options
|
88
115
|
#
|
89
116
|
# @api public
|
90
|
-
def parse(source,
|
91
|
-
|
92
|
-
|
117
|
+
def parse(source, width: TTY::Screen.width, theme: THEME, indent: 2,
|
118
|
+
mode: TTY::Color.mode, symbols: {}, color: :auto,
|
119
|
+
**doc_opts)
|
120
|
+
convert_options = { width: width, indent: indent, theme: theme,
|
121
|
+
mode: mode, symbols: build_symbols(symbols),
|
122
|
+
input: "KramdownExt", enabled: color_enabled(color) }
|
123
|
+
doc = Kramdown::Document.new(source, convert_options.merge(doc_opts))
|
124
|
+
Converter.convert(doc.root, doc.options).join
|
93
125
|
end
|
94
126
|
module_function :parse
|
95
127
|
|
@@ -97,18 +129,41 @@ module TTY
|
|
97
129
|
#
|
98
130
|
# @api public
|
99
131
|
def parse_file(path, **options)
|
100
|
-
parse(::File.read(path), options)
|
132
|
+
parse(::File.read(path), **options)
|
101
133
|
end
|
102
134
|
module_function :parse_file
|
103
135
|
|
104
|
-
|
105
|
-
|
136
|
+
# Convert color setting to Pastel setting
|
137
|
+
#
|
138
|
+
# @api private
|
139
|
+
def color_enabled(color)
|
140
|
+
case color.to_s
|
141
|
+
when "always" then true
|
142
|
+
when "never" then false
|
143
|
+
else nil
|
144
|
+
end
|
106
145
|
end
|
107
|
-
module_function :
|
146
|
+
module_function :color_enabled
|
147
|
+
private_class_method :color_enabled
|
108
148
|
|
109
|
-
|
110
|
-
|
149
|
+
# Extract and build symbols
|
150
|
+
#
|
151
|
+
# @api private
|
152
|
+
def build_symbols(options)
|
153
|
+
if options == :ascii
|
154
|
+
ASCII_SYMBOLS
|
155
|
+
elsif options.is_a?(Hash)
|
156
|
+
base_symbols = options[:base] == :ascii ? ASCII_SYMBOLS : SYMBOLS
|
157
|
+
if options[:override].is_a?(Hash)
|
158
|
+
base_symbols.merge(options[:override])
|
159
|
+
else
|
160
|
+
base_symbols
|
161
|
+
end
|
162
|
+
else
|
163
|
+
SYMBOLS
|
164
|
+
end
|
111
165
|
end
|
112
|
-
module_function :
|
166
|
+
module_function :build_symbols
|
167
|
+
private_class_method :build_symbols
|
113
168
|
end # Markdown
|
114
169
|
end # TTY
|