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.
@@ -0,0 +1,159 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "error"
4
+
5
+ module TTY
6
+ class Markdown
7
+ # Responsible for storing the theme configuration
8
+ #
9
+ # @api private
10
+ class Theme
11
+ # The element to style hash
12
+ #
13
+ # @return [Hash{Symbol => Array<Symbol>}]
14
+ #
15
+ # @api private
16
+ ELEMENT_TO_STYLE = {
17
+ code: %i[yellow],
18
+ comment: %i[bright_black],
19
+ delete: %i[red],
20
+ em: %i[yellow],
21
+ h1: %i[cyan bold underline],
22
+ h2: %i[cyan bold],
23
+ h3: %i[cyan bold],
24
+ h4: %i[cyan bold],
25
+ h5: %i[cyan bold],
26
+ h6: %i[cyan bold],
27
+ hr: %i[yellow],
28
+ image: %i[bright_black],
29
+ link: %i[yellow underline],
30
+ list: %i[yellow],
31
+ note: %i[yellow],
32
+ quote: %i[yellow],
33
+ strong: %i[yellow bold],
34
+ table: %i[yellow]
35
+ }.freeze
36
+ private_constant :ELEMENT_TO_STYLE
37
+
38
+ # Create a {TTY::Markdown::Theme} instance
39
+ #
40
+ # @example
41
+ # theme = TTY::Markdown::Theme.from({comment: :blue})
42
+ #
43
+ # @example
44
+ # theme = TTY::Markdown::Theme.from({comment: %i[blue underline]})
45
+ #
46
+ # @param [Hash{Symbol => Array<String, Symbol>, String, Symbol}] theme
47
+ # the theme configuration
48
+ #
49
+ # @return [TTY::Markdown::Theme]
50
+ #
51
+ # @raise [TTY::Markdown::Error]
52
+ # when the theme value is invalid
53
+ #
54
+ # @api public
55
+ def self.from(theme)
56
+ new(validate_names(build_theme(theme)))
57
+ end
58
+
59
+ # Build the theme hash
60
+ #
61
+ # @param [Hash{Symbol => Array<String, Symbol>, String, Symbol}] theme
62
+ # the theme configuration
63
+ #
64
+ # @return [Hash{Symbol => Array<Symbol>}]
65
+ #
66
+ # @raise [TTY::Markdown::Error]
67
+ # when the theme value is invalid
68
+ #
69
+ # @api private
70
+ def self.build_theme(theme)
71
+ raise_value_error(theme) unless theme.respond_to?(:to_h)
72
+
73
+ ELEMENT_TO_STYLE.merge(theme.to_h) do |*, new_style|
74
+ Array(new_style).map(&:to_sym)
75
+ end
76
+ end
77
+ private_class_method :build_theme
78
+
79
+ # Validate the elements names
80
+ #
81
+ # @param [Hash{Symbol => Array<Symbol>}] value
82
+ # the theme value
83
+ #
84
+ # @return [Hash{Symbol => Array<Symbol>}]
85
+ #
86
+ # @raise [TTY::Markdown::Error]
87
+ # when the element name is invalid
88
+ #
89
+ # @api private
90
+ def self.validate_names(value)
91
+ unknown_names = value.keys - ELEMENT_TO_STYLE.keys
92
+ return value if unknown_names.empty?
93
+
94
+ raise_name_error(*unknown_names)
95
+ end
96
+ private_class_method :validate_names
97
+
98
+ # Raise the theme value error
99
+ #
100
+ # @param [Object] value
101
+ # the theme value
102
+ #
103
+ # @return [void]
104
+ #
105
+ # @raise [TTY::Markdown::Error]
106
+ # when the theme value is invalid
107
+ #
108
+ # @api private
109
+ def self.raise_value_error(value)
110
+ raise Error, "invalid theme: #{value.inspect}. " \
111
+ "Use the hash with the element name and style."
112
+ end
113
+ private_class_method :raise_value_error
114
+
115
+ # Raise the element name error
116
+ #
117
+ # @param [Array<Symbol>] names
118
+ # the elements names
119
+ #
120
+ # @return [void]
121
+ #
122
+ # @raise [TTY::Markdown::Error]
123
+ # when the element name is invalid
124
+ #
125
+ # @api private
126
+ def self.raise_name_error(*names)
127
+ raise Error, "invalid theme element name#{"s" if names.size > 1}: " \
128
+ "#{names.map(&:inspect).join(", ")}."
129
+ end
130
+ private_class_method :raise_name_error
131
+
132
+ # Create a {TTY::Markdown::Theme} instance
133
+ #
134
+ # @param [Hash{Symbol => Array<Symbol>}] theme
135
+ # the theme configuration
136
+ #
137
+ # @api private
138
+ def initialize(theme)
139
+ @theme = theme
140
+ end
141
+ private_class_method :new
142
+
143
+ # Fetch styles by element name
144
+ #
145
+ # @example
146
+ # theme[:comment]
147
+ #
148
+ # @param [Symbol] name
149
+ # the element name
150
+ #
151
+ # @return [Array<Symbol>]
152
+ #
153
+ # @api public
154
+ def [](name)
155
+ @theme[name]
156
+ end
157
+ end # Theme
158
+ end # Markdown
159
+ end # TTY
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TTY
4
+ class Markdown
5
+ VERSION = "0.7.2"
6
+ end # TTY
7
+ end # Markdown
@@ -0,0 +1,165 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "kramdown/document"
4
+ require "tty-color"
5
+ require "tty-screen"
6
+
7
+ require_relative "markdown/color"
8
+ require_relative "markdown/converter"
9
+ require_relative "markdown/parser"
10
+ require_relative "markdown/symbols"
11
+ require_relative "markdown/theme"
12
+ require_relative "markdown/version"
13
+
14
+ module TTY
15
+ # Responsible for converting Markdown to the terminal output
16
+ #
17
+ # @api public
18
+ class Markdown
19
+ # The input parser name
20
+ #
21
+ # @return [String]
22
+ #
23
+ # @api private
24
+ INPUT_PARSER = "TTYMarkdown"
25
+ private_constant :INPUT_PARSER
26
+
27
+ # Parse Markdown content
28
+ #
29
+ # @example
30
+ # TTY::Markdown.parse("# TTY Toolkit")
31
+ #
32
+ # @example
33
+ # TTY::Markdown.parse("# TTY Toolkit", mode: 16)
34
+ #
35
+ # @param [String] content
36
+ # the Markdown content
37
+ # @param [Hash] options
38
+ # the conversion options
39
+ #
40
+ # @return [String]
41
+ # the converted terminal output
42
+ #
43
+ # @raise [TTY::Markdown::Error]
44
+ # when the option value is invalid
45
+ #
46
+ # @see #initialize
47
+ #
48
+ # @api public
49
+ def self.parse(content, **options)
50
+ new(**options).parse(content)
51
+ end
52
+
53
+ # Parse a Markdown file
54
+ #
55
+ # @example
56
+ # TTY::Markdown.parse_file("example.md")
57
+ #
58
+ # @example
59
+ # TTY::Markdown.parse_file("example.md", mode: 16)
60
+ #
61
+ # @param [String] path
62
+ # the Markdown file path
63
+ # @param [Hash] options
64
+ # the conversion options
65
+ #
66
+ # @return [String]
67
+ # the converted terminal output
68
+ #
69
+ # @raise [TTY::Markdown::Error]
70
+ # when the option value is invalid
71
+ #
72
+ # @see #initialize
73
+ #
74
+ # @api public
75
+ def self.parse_file(path, **options)
76
+ new(**options).parse_file(path)
77
+ end
78
+
79
+ # Create a {TTY::Markdown} instance
80
+ #
81
+ # @example
82
+ # tty_markdown = TTY::Markdown.new
83
+ #
84
+ # @example
85
+ # tty_markdown = TTY::Markdown.new(mode: 16)
86
+ #
87
+ # @example
88
+ # tty_markdown = TTY::Markdown.new(symbols: :ascii)
89
+ #
90
+ # @example
91
+ # tty_markdown = TTY::Markdown.new(theme: {link: :blue})
92
+ #
93
+ # @param [String, Symbol] color
94
+ # the color support out of always, auto or never
95
+ # @param [Integer] indent
96
+ # the output indent
97
+ # @param [Integer] mode
98
+ # the color mode
99
+ # @param [Hash, String, Symbol, nil] symbols
100
+ # the output symbols
101
+ # @param [Hash{Symbol => Array, String, Symbol}, nil] theme
102
+ # the color theme
103
+ # @param [Integer] width
104
+ # the maximum width
105
+ # @param [Hash] document_options
106
+ # the document parser options
107
+ #
108
+ # @raise [TTY::Markdown::Error]
109
+ # when the option value is invalid
110
+ #
111
+ # @api public
112
+ def initialize(
113
+ color: :auto,
114
+ indent: 2,
115
+ mode: TTY::Color.mode,
116
+ symbols: {},
117
+ theme: {},
118
+ width: TTY::Screen.width,
119
+ **document_options
120
+ )
121
+ @converter_options = {
122
+ enabled: Color.new(color).to_enabled,
123
+ indent: indent,
124
+ input: INPUT_PARSER,
125
+ mode: mode,
126
+ symbols: Symbols.from(symbols),
127
+ theme: Theme.from(theme),
128
+ width: width
129
+ }.merge(document_options)
130
+ end
131
+
132
+ # Parse Markdown content
133
+ #
134
+ # @example
135
+ # tty_markdown.parse("# TTY Toolkit")
136
+ #
137
+ # @param [String] content
138
+ # the Markdown content
139
+ #
140
+ # @return [String]
141
+ # the converted terminal output
142
+ #
143
+ # @api public
144
+ def parse(content)
145
+ document = Kramdown::Document.new(content, @converter_options)
146
+ Converter.convert(document.root, document.options).join
147
+ end
148
+
149
+ # Parse a Markdown file
150
+ #
151
+ # @example
152
+ # tty_markdown.parse_file("example.md")
153
+ #
154
+ # @param [String] path
155
+ # the Markdown file path
156
+ #
157
+ # @return [String]
158
+ # the converted terminal output
159
+ #
160
+ # @api public
161
+ def parse_file(path)
162
+ parse(::File.read(path))
163
+ end
164
+ end # Markdown
165
+ end # TTY
@@ -0,0 +1 @@
1
+ require_relative "tty/markdown"
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tty-markdown-meinac
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.2
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Murach
8
+ - Mehmet Emin INAC
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 1980-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kramdown
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.16.2
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 1.16.2
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: pastel
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.8'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.8'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rouge
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '3.14'
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '6.0'
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '3.14'
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '6.0'
67
+ - !ruby/object:Gem::Dependency
68
+ name: strings
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: 0.2.0
74
+ type: :runtime
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: 0.2.0
81
+ - !ruby/object:Gem::Dependency
82
+ name: tty-color
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '0.6'
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '0.6'
95
+ - !ruby/object:Gem::Dependency
96
+ name: tty-screen
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '0.8'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '0.8'
109
+ - !ruby/object:Gem::Dependency
110
+ name: rake
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ - !ruby/object:Gem::Dependency
124
+ name: rspec
125
+ requirement: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '3.0'
130
+ type: :development
131
+ prerelease: false
132
+ version_requirements: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '3.0'
137
+ description: Convert a Markdown text or document into a terminal friendly output.
138
+ email:
139
+ - piotr@piotrmurach.com
140
+ executables: []
141
+ extensions: []
142
+ extra_rdoc_files:
143
+ - CHANGELOG.md
144
+ - LICENSE.txt
145
+ - README.md
146
+ files:
147
+ - CHANGELOG.md
148
+ - LICENSE.txt
149
+ - README.md
150
+ - lib/tty-markdown.rb
151
+ - lib/tty/markdown.rb
152
+ - lib/tty/markdown/color.rb
153
+ - lib/tty/markdown/converter.rb
154
+ - lib/tty/markdown/decorator.rb
155
+ - lib/tty/markdown/error.rb
156
+ - lib/tty/markdown/formatter.rb
157
+ - lib/tty/markdown/highlighter.rb
158
+ - lib/tty/markdown/parser.rb
159
+ - lib/tty/markdown/symbols.rb
160
+ - lib/tty/markdown/theme.rb
161
+ - lib/tty/markdown/version.rb
162
+ homepage: https://ttytoolkit.org
163
+ licenses:
164
+ - MIT
165
+ metadata:
166
+ allowed_push_host: https://rubygems.org
167
+ bug_tracker_uri: https://github.com/piotrmurach/tty-markdown/issues
168
+ changelog_uri: https://github.com/piotrmurach/tty-markdown/blob/master/CHANGELOG.md
169
+ documentation_uri: https://www.rubydoc.info/gems/tty-markdown
170
+ funding_uri: https://github.com/sponsors/piotrmurach
171
+ homepage_uri: https://ttytoolkit.org
172
+ rubygems_mfa_required: 'true'
173
+ source_code_uri: https://github.com/piotrmurach/tty-markdown
174
+ rdoc_options: []
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: 2.0.0
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ requirements: []
188
+ rubygems_version: 4.0.10
189
+ specification_version: 4
190
+ summary: Convert a Markdown text or document into a terminal friendly output.
191
+ test_files: []