tty-markdown-meinac 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '048f174ebf3a7a252c876b74c92f4770c662fffbc1dcaeebfd994e366fb3455c'
4
+ data.tar.gz: 9990524f7ef5c9152ffc65825e4652847eeae4275b646844694c3aecbdf95a2e
5
+ SHA512:
6
+ metadata.gz: 6103734b3e3629f3e47cd77f7122a331d16bc56a1e02759bb7dcdb826c442db6b31a21734d69720c149161e6a3880c27c3d383614fa4070b0d6556434355a748
7
+ data.tar.gz: 263ad5d46256315dac06fc59e7494f4b9c695ecb10d78391295b1e958a33014821ca5d25894cbc5598e29fe6f947045d8d74841f1f44ca03de22ef83e83ec52f
data/CHANGELOG.md ADDED
@@ -0,0 +1,157 @@
1
+ # Change log
2
+
3
+ ## unreleased
4
+
5
+ ### Added
6
+
7
+ * Add the ability to instantiate the `TTY::Markdown`.
8
+ * Add the `code`, `delete`, `h1`, `h2`, `h3`, `h4`, `h5` and `h6` styles
9
+ configuration to the default theme.
10
+ * Add the ability to format code elements in truecolor.
11
+
12
+ ### Changed
13
+
14
+ * Change the `TTY::Markdown` instantiation to raise the `TTY::Markdown::Error`
15
+ when configuring an invalid `color`, `symbols` or `theme` option value.
16
+ * Change the code block conversion to always append a newline.
17
+ * Change the del conversion to replace the `delete` symbol with
18
+ the theme `delete` style.
19
+
20
+ ### Fixed
21
+
22
+ * Fix the indentation of multiline paragraphs with formatted elements.
23
+
24
+ ### Removed
25
+
26
+ * Remove the `header` style from the theme configuration.
27
+
28
+ ## [v0.7.2] - 2023-03-12
29
+
30
+ ### Changed
31
+
32
+ * Change gemspec to expand supported rouge versions by Ash McKenzie(@ashmckenzie)
33
+
34
+ ## [v0.7.1] - 2022-12-21
35
+
36
+ ### Changed
37
+
38
+ * Change the symbols option to accept string value
39
+
40
+ ### Fixed
41
+
42
+ * Fix the theme option to allow overriding specific markdown element styles
43
+
44
+ ## [v0.7.0] - 2020-09-03
45
+
46
+ ### Added
47
+
48
+ * Add converter for br element by @krage
49
+ * Add configuration of symbols by @krage
50
+ * Add definition list support
51
+ * Add table footer formatting
52
+ * Add formatting of image source location
53
+ * Add footnotes support
54
+ * Add XML comments support
55
+ * Add HTML div/i/em/b/strong/img/a element support
56
+ * Add color configuration setting
57
+
58
+ ### Changed
59
+
60
+ * Change #new to accept configuration options as keywords
61
+ * Display links with both label and optional title by @krage
62
+ * Display link without the label when label content is same as link target @krage
63
+ * Change hr formatting to be always full width
64
+ * Differentiate between span and block math elements
65
+ * Display abbreviation with its definition
66
+ * Improve performance by calculating table column widths and row heights only once
67
+ * Change to remove mailto: from email links for brevity @krage
68
+ * Change to update kramdown to support versions >= 1.16 and < 3.0
69
+ * Change to update pastel, tty-color & tty-screen dependencies
70
+ * Change HTML del element formatting to strikethrough
71
+ * Rename colors setting to mode
72
+
73
+ ### Fixed
74
+
75
+ * Fix break line handling by @krage
76
+ * Fix links handling by @krage
77
+ * Fix table formatting of empty cells
78
+ * Fix content wrapping to account for the current indentation
79
+ * Fix header wrapping
80
+ * Fix blockquote formatting of content with emphasised style or apostrophe
81
+ * Fix support for HTML del element
82
+
83
+ ## [v0.6.0] - 2019-03-30
84
+
85
+ ### Added
86
+
87
+ * Add markdown xml comments conversion
88
+
89
+ ## [v0.5.1] - 2019-02-07
90
+
91
+ ### Fixed
92
+
93
+ * Fix spaces around inline code quotes collapses inside list items
94
+
95
+ ## [v0.5.0] - 2018-12-13
96
+
97
+ ### Changed
98
+
99
+ * Change gemspec to load files directly
100
+ * Change to update rouge dependency
101
+ * Change to relax constraints on tty-screen and tty-color
102
+
103
+ ## [v0.4.0] - 2018-06-20
104
+
105
+ ## Fixed
106
+
107
+ * Fix multiline paragraph indentation by Brett(@suwyn)
108
+
109
+ ## [v0.3.0] - 2018-03-17
110
+
111
+ ### Added
112
+
113
+ * Add :width option to allow setting maximum display width
114
+ * Add :colors options for specifying rendering colors capabilities
115
+ * Add ability to parse multiline table content
116
+
117
+ ### Changed
118
+
119
+ * Change color scheme to replace table and links blue with yellow
120
+
121
+ ## Fixed
122
+
123
+ * Fix issue with multiline blockquote elements raising NoMethodError
124
+
125
+ ## [v0.2.0] - 2018-01-29
126
+
127
+ ### Added
128
+
129
+ * Add space indented codeblock markdown conversion
130
+ * Add markdown math formula conversion
131
+ * Add markdown typogrpahic symbols conversion
132
+ by Tanaka Masaki(@T-a-n-a-k-a-M-a-s-a-k-i)
133
+ * Add html entities conversion
134
+ * Add warnings about unsupported conversions for completeness
135
+
136
+ ### Changed
137
+
138
+ * Change gemspec to require Ruby >= 2.0.0
139
+
140
+ ### Fixed
141
+
142
+ * Fix smart quotes to correctly encode entities
143
+
144
+ ## [v0.1.0] - 2018-01-24
145
+
146
+ * Initial implementation and release
147
+
148
+ [v0.7.2]: https://github.com/piotrmurach/tty-markdown/compare/v0.7.1...v0.7.2
149
+ [v0.7.1]: https://github.com/piotrmurach/tty-markdown/compare/v0.7.0...v0.7.1
150
+ [v0.7.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.6.0...v0.7.0
151
+ [v0.6.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.5.1...v0.6.0
152
+ [v0.5.1]: https://github.com/piotrmurach/tty-markdown/compare/v0.5.0...v0.5.1
153
+ [v0.5.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.4.0...v0.5.0
154
+ [v0.4.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.3.0...v0.4.0
155
+ [v0.3.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.2.0...v0.3.0
156
+ [v0.2.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.1.0...v0.2.0
157
+ [v0.1.0]: https://github.com/piotrmurach/tty-markdown/compare/v0.1.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Piotr Murach (piotrmurach.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,414 @@
1
+ <div align="center">
2
+ <a href="https://ttytoolkit.org"><img width="130" src="https://github.com/piotrmurach/tty/raw/master/images/tty.png" alt="TTY Toolkit logo" /></a>
3
+ </div>
4
+
5
+ # TTY::Markdown
6
+
7
+ [![Gem Version](https://badge.fury.io/rb/tty-markdown.svg)][gem]
8
+ [![Actions CI](https://github.com/piotrmurach/tty-markdown/actions/workflows/ci.yml/badge.svg)][gh_actions_ci]
9
+ [![Build status](https://ci.appveyor.com/api/projects/status/k4vub4koct329ggd?svg=true)][appveyor]
10
+ [![Maintainability](https://qlty.sh/gh/piotrmurach/projects/tty-markdown/maintainability.svg)][qlty]
11
+ [![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-markdown/badge.svg)][coverage]
12
+
13
+ [gem]: https://badge.fury.io/rb/tty-markdown
14
+ [gh_actions_ci]: https://github.com/piotrmurach/tty-markdown/actions/workflows/ci.yml
15
+ [appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-markdown
16
+ [qlty]: https://qlty.sh/gh/piotrmurach/projects/tty-markdown
17
+ [coverage]: https://coveralls.io/github/piotrmurach/tty-markdown
18
+
19
+ > Convert a Markdown document or text into a terminal friendly output.
20
+
21
+ **TTY::Markdown** provides a Markdown processing component for the
22
+ [TTY Toolkit](https://github.com/piotrmurach/tty).
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ ```ruby
29
+ gem "tty-markdown"
30
+ ```
31
+
32
+ And then execute:
33
+
34
+ ```shell
35
+ $ bundle
36
+ ```
37
+
38
+ Or install it yourself as:
39
+
40
+ ```shell
41
+ $ gem install tty-markdown
42
+ ```
43
+
44
+ ## Contents
45
+
46
+ * [1. Usage](#1-usage)
47
+ * [1.1 Header](#11-header)
48
+ * [1.2 List](#12-list)
49
+ * [1.3 Definition List](#13-definition-list)
50
+ * [1.4 Link](#14-link)
51
+ * [1.5 Blockquote](#15-blockquote)
52
+ * [1.6 Code and Syntax Highlighting](#16-code-and-syntax-highlighting)
53
+ * [1.7 Table](#17-table)
54
+ * [1.8 Horizontal Rule](#18-horizontal-rule)
55
+ * [1.9 Footnotes](#19-footnotes)
56
+ * [2. Options](#2-options)
57
+ * [2.1 :mode](#21-mode)
58
+ * [2.2 :theme](#22-theme)
59
+ * [2.3 :width](#23-width)
60
+ * [2.4 :symbols](#24-symbols)
61
+ * [2.5 :indent](#25-indent)
62
+ * [2.6 :color](#26-color)
63
+ * [3. Command line tool](#3-command-line-tool)
64
+
65
+ ## 1. Usage
66
+
67
+ Using `parse` method, you can transform a Markdown string into a terminal
68
+ formatted content:
69
+
70
+ ```ruby
71
+ parsed = TTY::Markdown.parse("# Hello")
72
+ puts parsed
73
+ # => "\e[36;1mHello\e[0m\n"
74
+ ```
75
+
76
+ The `parse_file` allows you to transform a Markdown document into a terminal
77
+ formatted output:
78
+
79
+ ```ruby
80
+ parsed = TTY::Markdown.parse_file('example.md')
81
+ puts parsed
82
+ ```
83
+
84
+ ### 1.1 Header
85
+
86
+ Parsing the following Markdown headers:
87
+
88
+ ```markdown
89
+ TTY::Markdown
90
+ =============
91
+
92
+ **tty-markdown** converts markdown document into a terminal friendly output.
93
+
94
+ ## Examples
95
+
96
+ ### Nested list items
97
+ ```
98
+
99
+ The terminal output looks like this:
100
+
101
+ ![Headers example](assets/headers.png)
102
+
103
+ ### 1.2 List
104
+
105
+ Both numbered and unordered lists are supported. Given a Markdown:
106
+
107
+ ```markdown
108
+ - Item 1
109
+ - Item 2
110
+ - Item 3
111
+ - Item 4
112
+ - Item 5
113
+ ```
114
+
115
+ The parsed output looks like this:
116
+
117
+ ![Unordered list example](assets/list.png)
118
+
119
+ ### 1.3 Definition List
120
+
121
+ Given a definition list:
122
+
123
+ ```markdown
124
+ Item 1
125
+ : This is the description for Item 1
126
+
127
+ Item 2
128
+ : This is the description for Item 2
129
+ : This is another description for Item 2
130
+ ```
131
+
132
+ The parsed output looks like this:
133
+
134
+ ![Definition list example](assets/definition-list.png)
135
+
136
+ ### 1.4 Link
137
+
138
+ A Markdown link:
139
+
140
+ ```markdown
141
+ [An inline-style link](https://ttytoolkit.org)
142
+
143
+ [An inline-style link with title](https://ttytoolkit.org "TTY Toolkit Homepage")
144
+ ```
145
+
146
+ The link text will be rendered with the link next to it:
147
+
148
+ ![Link example](assets/link.png)
149
+
150
+ ### 1.5 Blockquote
151
+
152
+ Given a Markdown quote:
153
+
154
+ ```markdown
155
+ > Blockquotes are very handy in email to emulate reply text.
156
+ > This line is part of the same quote.
157
+ > *Oh*, you can put **Markdown** into a blockquote.
158
+ ```
159
+
160
+ The rendered output looks like this:
161
+
162
+ ![Blockquote example](assets/quote.png)
163
+
164
+ ### 1.6 Code and Syntax Highlighting
165
+
166
+ The parser can highlight syntax of many programming languages.
167
+
168
+ Given a Markdown codeblock with a language specification:
169
+
170
+ ````markdown
171
+ ```ruby
172
+ class Greeter
173
+ def hello(name)
174
+ puts "Hello #{name}"
175
+ end
176
+ end
177
+ ```
178
+ ````
179
+
180
+ The terminal output will look like this:
181
+
182
+ ![Code highlighting example](assets/codeblock.png)
183
+
184
+ ### 1.7 Table
185
+
186
+ You can transform tables which understand the Markdown alignment.
187
+
188
+ For example, given the following table:
189
+
190
+ ```markdown
191
+ | Tables | Are | Cool |
192
+ |----------|:-------------:|------:|
193
+ | col 1 is | left-aligned | $1600 |
194
+ | col 2 is | centered | $12 |
195
+ | col 3 is | right-aligned | $1 |
196
+ ```
197
+
198
+ Then the terminal output will look like this:
199
+
200
+ ![Table example](assets/table.png)
201
+
202
+ ### 1.8 Horizontal Rule
203
+
204
+ You can specify a horizontal rule in Markdown:
205
+
206
+ ```markdown
207
+ ***
208
+ ```
209
+
210
+ and then transform it:
211
+
212
+ ```ruby
213
+ parsed = TTY::Markdown.parse(markdown_string)
214
+ ```
215
+
216
+ `puts parsed` will output:
217
+
218
+ ![Horizontal rule example](assets/hr.png)
219
+
220
+ ### 1.9 Footnotes
221
+
222
+ You can create footnote references:
223
+
224
+ ```markdown
225
+ It is not down on any map[^foo]; true places[^bar] never are.
226
+
227
+ [^foo]: A diagrammatic representation of an area of land or sea.
228
+ [^bar]: A particular position, point, or area in space; a location.
229
+ ```
230
+
231
+ All footnotes will be displayed with a sequential number and rendered in
232
+ the terminal like this:
233
+
234
+ ![Footnotes example](assets/footnotes.png)
235
+
236
+ ## 2. Options
237
+
238
+ ### 2.1 `:mode`
239
+
240
+ By default the `256` color scheme is used to render code block elements.
241
+
242
+ Use the `:mode` keyword to specify the maximum number of colors, for example,
243
+ to be `16` ANSI colors:
244
+
245
+ ```ruby
246
+ TTY::Markdown.parse(markdown_string, mode: 16)
247
+ ```
248
+
249
+ This feature may be handy when working in terminals with limited color support.
250
+
251
+ By default, **TTY::Markdown** detects terminal color mode and adjusts output
252
+ automatically.
253
+
254
+ ### 2.2 `:theme`
255
+
256
+ Use the `:theme` keyword to change specific Markdown element styles.
257
+
258
+ For example, to override styles for the `link` and `list` elements do:
259
+
260
+ ```ruby
261
+ TTY::Markdown.parse(markdown_string, theme: {link: :magenta, list: %i[magenta bold]})
262
+ ```
263
+
264
+ Here's a complete list of element names with corresponding styles:
265
+
266
+ | Name | Style |
267
+ |-------------|---------------------------|
268
+ | `:code` | `%i[yellow]` |
269
+ | `:comment` | `%i[bright_black]` |
270
+ | `:delete` | `%i[red]` |
271
+ | `:em` | `%i[yellow]` |
272
+ | `:h1` | `%i[cyan bold underline]` |
273
+ | `:h2` | `%i[cyan bold]` |
274
+ | `:h3` | `%i[cyan bold]` |
275
+ | `:h4` | `%i[cyan bold]` |
276
+ | `:h5` | `%i[cyan bold]` |
277
+ | `:h6` | `%i[cyan bold]` |
278
+ | `:hr` | `%i[yellow]` |
279
+ | `:image` | `%i[bright_black]` |
280
+ | `:link` | `%i[yellow underline]` |
281
+ | `:list` | `%i[yellow]` |
282
+ | `:note` | `%i[yellow]` |
283
+ | `:quote` | `%i[yellow]` |
284
+ | `:strong` | `%i[yellow bold]` |
285
+ | `:table` | `%i[yellow]` |
286
+
287
+ Read the [pastel](https://github.com/piotrmurach/pastel#3-supported-colors)
288
+ documentation for all supported styles.
289
+
290
+ ### 2.3 `:width`
291
+
292
+ Use the `:width` keyword to control the maximum width of the output:
293
+
294
+ ```ruby
295
+ TTY::Markdown.parse(markdown_string, width: 80)
296
+ ```
297
+
298
+ By default the terminal screen width is used.
299
+
300
+ ### 2.4 `:symbols`
301
+
302
+ By default, formatted output includes various Unicode symbols. Use the
303
+ `:symbols` keyword to switch to an ASCII set and/or override individual
304
+ symbols.
305
+
306
+ ```ruby
307
+ TTY::Markdown.parse(markdown_string, symbols: :ascii)
308
+ TTY::Markdown.parse(markdown_string, symbols: {base: :ascii})
309
+ TTY::Markdown.parse(markdown_string, symbols: {override: {bullet: "x"}})
310
+ ```
311
+
312
+ Here's a complete list of symbol names with corresponding ASCII and Unicode
313
+ characters:
314
+
315
+ | Name | ASCII | Unicode |
316
+ |------------------|-------|---------|
317
+ | `:arrow` | `->` | `»` |
318
+ | `:bar` | `\|` | `┃` |
319
+ | `:bottom_center` | `+` | `┴` |
320
+ | `:bottom_left` | `+` | `└` |
321
+ | `:bottom_right` | `+` | `┘` |
322
+ | `:bracket_left` | `[` | `[` |
323
+ | `:bracket_right` | `]` | `]` |
324
+ | `:bullet` | `*` | `●` |
325
+ | `:diamond` | `*` | `◈` |
326
+ | `:hash` | `#` | `#` |
327
+ | `:hellip` | `...` | `…` |
328
+ | `:laquo` | `<<` | `«` |
329
+ | `:laquo_space` | `<< ` | `« ` |
330
+ | `:ldquo` | `"` | `“` |
331
+ | `:lsquo` | `"` | `‘` |
332
+ | `:line` | `-` | `─` |
333
+ | `:mdash` | `-` | `—` |
334
+ | `:mid_center` | `+` | `┼` |
335
+ | `:mid_left` | `+` | `├` |
336
+ | `:mid_right` | `+` | `┤` |
337
+ | `:ndash` | `-` | `-` |
338
+ | `:paren_left` | `(` | `(` |
339
+ | `:paren_right` | `)` | `)` |
340
+ | `:pipe` | `\|` | `│` |
341
+ | `:raquo` | `>>` | `»` |
342
+ | `:raquo_space` | ` >>` | ` »` |
343
+ | `:rdquo` | `"` | `”` |
344
+ | `:rsquo` | `"` | `’` |
345
+ | `:top_center` | `+` | `┬` |
346
+ | `:top_left` | `+` | `┌` |
347
+ | `:top_right` | `+` | `┐` |
348
+
349
+ ### 2.5 `:indent`
350
+
351
+ By default, any content apart from the main `h1` header is indented with `2`
352
+ spaces. Use the `:indent` keyword to provide custom indent or no indent at all:
353
+
354
+ ```ruby
355
+ TTY::Markdown.parse(markdown_string, indent: 0)
356
+ ```
357
+
358
+ ### 2.6 `:color`
359
+
360
+ Use the `:color` keyword to control when to apply coloring to various document
361
+ elements. Valid values are `:always`, `:auto` or `:never`. The default `:auto`
362
+ value automatically detects whether to color the output or not.
363
+
364
+ For example, to always color content regardless of terminal support:
365
+
366
+ ```ruby
367
+ TTY::Markdown.parse(markdown_string, color: :always)
368
+ ```
369
+
370
+ ### 3. Command line tool
371
+
372
+ Install the [tty-markdown-cli](https://github.com/piotrmurach/tty-markdown-cli)
373
+ to use the `tty-markdown` executable in the terminal:
374
+
375
+ ```bash
376
+ $ tty-markdown README.md
377
+ ```
378
+
379
+ ## Development
380
+
381
+ After checking out the repo, run `bin/setup` to install dependencies.
382
+ Then, run `rake spec` to run the tests. You can also run `bin/console`
383
+ for an interactive prompt that will allow you to experiment.
384
+
385
+ To install this gem onto your local machine, run `bundle exec rake install`.
386
+ To release a new version, update the version number in `version.rb`, and then
387
+ run `bundle exec rake release`, which will create a git tag for the version,
388
+ push git commits and tags, and push the `.gem` file to
389
+ [rubygems.org](https://rubygems.org).
390
+
391
+ ## Contributing
392
+
393
+ Bug reports and pull requests are welcome on GitHub at
394
+ https://github.com/piotrmurach/tty-markdown.
395
+ This project is intended to be a safe, welcoming space for collaboration,
396
+ and contributors are expected to adhere to the
397
+ [code of conduct](https://github.com/piotrmurach/tty-markdown/blob/master/CODE_OF_CONDUCT.md).
398
+
399
+ ## License
400
+
401
+ The gem is available as open source under the terms of the
402
+ [MIT License](https://opensource.org/licenses/MIT).
403
+
404
+ ## Code of Conduct
405
+
406
+ Everyone interacting in the TTY::Markdown project’s codebases, issue trackers,
407
+ chat rooms and mailing lists is expected to follow the
408
+ [code of conduct](https://github.com/piotrmurach/tty-markdown/blob/master/CODE_OF_CONDUCT.md).
409
+
410
+ ## Copyright
411
+
412
+ Copyright (c) 2018 Piotr Murach. See
413
+ [LICENSE.txt](https://github.com/piotrmurach/tty-markdown/blob/master/LICENSE.txt)
414
+ for further details.
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "error"
4
+
5
+ module TTY
6
+ class Markdown
7
+ # Responsible for storing the color configuration
8
+ #
9
+ # @api private
10
+ class Color
11
+ # The always name
12
+ #
13
+ # @return [String]
14
+ #
15
+ # @api private
16
+ ALWAYS = "always"
17
+ private_constant :ALWAYS
18
+
19
+ # The auto name
20
+ #
21
+ # @return [String]
22
+ #
23
+ # @api private
24
+ AUTO = "auto"
25
+ private_constant :AUTO
26
+
27
+ # The never name
28
+ #
29
+ # @return [String]
30
+ #
31
+ # @api private
32
+ NEVER = "never"
33
+ private_constant :NEVER
34
+
35
+ # The allowed modes
36
+ #
37
+ # @return [Array<String>]
38
+ #
39
+ # @api private
40
+ MODES = [ALWAYS, AUTO, NEVER].freeze
41
+ private_constant :MODES
42
+
43
+ # Create a {TTY::Markdown::Color} instance
44
+ #
45
+ # @example
46
+ # color = TTY::Markdown::Color.new(:always)
47
+ #
48
+ # @param [String, Symbol] color
49
+ # the color configuration
50
+ #
51
+ # @raise [TTY::Markdown::Error]
52
+ # when the color is invalid
53
+ #
54
+ # @api public
55
+ def initialize(color)
56
+ @color = validate(color)
57
+ end
58
+
59
+ # Convert to the Pastel enabled option
60
+ #
61
+ # @example
62
+ # color.to_enabled
63
+ #
64
+ # @return [Boolean, nil]
65
+ #
66
+ # @api public
67
+ def to_enabled
68
+ case @color.to_s
69
+ when ALWAYS then true
70
+ when NEVER then false
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ # Validate the color value
77
+ #
78
+ # @param [String, Symbol] value
79
+ # the color value
80
+ #
81
+ # @return [String, Symbol]
82
+ #
83
+ # @raise [TTY::Markdown::Error]
84
+ # when the color value is invalid
85
+ #
86
+ # @api private
87
+ def validate(value)
88
+ return value if MODES.include?(value.to_s)
89
+
90
+ raise_value_error(value)
91
+ end
92
+
93
+ # Raise the color value error
94
+ #
95
+ # @param [Object] value
96
+ # the color value
97
+ #
98
+ # @return [void]
99
+ #
100
+ # @raise [TTY::Markdown::Error]
101
+ # when the color value is invalid
102
+ #
103
+ # @api private
104
+ def raise_value_error(value)
105
+ raise Error, "invalid color: #{value.inspect}. Use the " \
106
+ ":#{ALWAYS}, :#{AUTO} or :#{NEVER} value."
107
+ end
108
+ end # Color
109
+ end # Markdown
110
+ end # TTY