dispersion 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ec8d56fde7ef494b2635dcedec943cf152f726d933f689b2cadaf5c86103281d
4
+ data.tar.gz: 35634702904c1aa084fdac4c2bf7c433bafa25686ee2de9cea476a08cdcef9e7
5
+ SHA512:
6
+ metadata.gz: 2e4ebdf10603779a04c21a43e6914c79ce6ddf9372e069058172ffb7db7e1eaaf6f1994a65d847813b842673cb8e9f129bcd197e47f936d6c46bff5c5562da45
7
+ data.tar.gz: 0d7ec6d70fb9ec42e5ad9f2e8fd890e5ce1665c026d48f719abb24a723105143504dfd80b73180d85968cac5ba8ec976195163755f21d786247b938a34f90fdb
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-10-09
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at joel@drapper.me. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in dispersion.gemspec
6
+ gemspec
7
+
8
+ gem "green_dots", path: "../green_dots"
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: ../green_dots
3
+ specs:
4
+ green_dots (0.1.0)
5
+ concurrent-ruby
6
+ prism
7
+ rouge
8
+ tty-tree
9
+ zeitwerk
10
+
11
+ PATH
12
+ remote: .
13
+ specs:
14
+ dispersion (0.1.0)
15
+ prism (~> 0.18)
16
+
17
+ GEM
18
+ remote: https://rubygems.org/
19
+ specs:
20
+ concurrent-ruby (1.2.2)
21
+ prism (0.18.0)
22
+ rouge (4.1.3)
23
+ tty-tree (0.4.0)
24
+ zeitwerk (2.6.12)
25
+
26
+ PLATFORMS
27
+ arm64-darwin-22
28
+
29
+ DEPENDENCIES
30
+ dispersion!
31
+ green_dots!
32
+
33
+ BUNDLED WITH
34
+ 2.4.12
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Joel Drapper
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,39 @@
1
+ # Dispersion
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dispersion`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dispersion. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/dispersion/blob/master/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the Dispersion project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/dispersion/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/dispersion/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "dispersion"
7
+ spec.version = Dispersion::VERSION
8
+ spec.authors = ["Joel Drapper"]
9
+ spec.email = ["joel@drapper.me"]
10
+
11
+ spec.summary = "Prism-powered syntax highlighter for Ruby"
12
+ spec.homepage = "https://github.com/joeldrapper/dispersion"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.0.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "https://github.com/joeldrapper/dispersion/blob/main/CHANGELOG.md"
19
+ spec.metadata["funding_uri"] = "https://github.com/sponsors/joeldrapper"
20
+
21
+ spec.files = Dir.chdir(__dir__) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
24
+ end
25
+ end
26
+
27
+ spec.require_paths = ["lib"]
28
+ spec.add_dependency "prism", "~> 0.18"
29
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dispersion
4
+ class Annotation
5
+ include Comparable
6
+
7
+ def initialize(type, location)
8
+ @type = type
9
+ @location = location
10
+ end
11
+
12
+ attr_reader :type, :location
13
+
14
+ def <=>(other)
15
+ comparison_value <=> other.comparison_value
16
+ end
17
+
18
+ def comparison_value
19
+ [
20
+ location,
21
+ (type == :reset ? 0 : 1)
22
+ ]
23
+ end
24
+
25
+ def to_ary
26
+ [type, location]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,333 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ module Dispersion
6
+ class Annotator < Prism::Visitor
7
+ def self.call(code)
8
+ new(code).call
9
+ end
10
+
11
+ def initialize(code)
12
+ @code = code
13
+ @annotations = Array.new(code.count("\n")) { [] }
14
+ end
15
+
16
+ def call
17
+ parsed = Prism.parse(@code)
18
+ parsed.comments.each { |c| visit_comment(c) }
19
+ visit(parsed.value)
20
+
21
+ @annotations.map(&:sort!)
22
+ end
23
+
24
+ def highlight(type, loc)
25
+ return unless loc
26
+ return if loc.start_line == loc.end_line && loc.start_column == loc.end_column
27
+
28
+ @annotations[loc.start_line - 1] << Dispersion::Annotation.new(
29
+ type,
30
+ loc.start_character_column,
31
+ )
32
+
33
+ @annotations[loc.end_line - 1] << Dispersion::Annotation.new(
34
+ :reset,
35
+ loc.end_character_column,
36
+ )
37
+ end
38
+
39
+ def visit_comment(node)
40
+ highlight :comment, node.location
41
+ end
42
+
43
+ def visit_def_node(node)
44
+ highlight :keyword, node.def_keyword_loc
45
+ highlight :keyword, node.end_keyword_loc
46
+ highlight :call, node.name_loc
47
+ highlight :bracket, node.lparen_loc
48
+ highlight :bracket, node.rparen_loc
49
+ super
50
+ end
51
+
52
+ def visit_local_variable_write_node(node)
53
+ highlight :var, node.name_loc
54
+ highlight :operator, node.operator_loc
55
+ super
56
+ end
57
+
58
+ def visit_local_variable_read_node(node)
59
+ highlight :var, node.location
60
+ super
61
+ end
62
+
63
+ def visit_instance_variable_write_node(node)
64
+ highlight :ivar, node.name_loc
65
+ highlight :operator, node.operator_loc
66
+ super
67
+ end
68
+
69
+ def visit_instance_variable_and_write_node(node)
70
+ highlight :ivar, node.name_loc
71
+ highlight :operator, node.operator_loc
72
+ super
73
+ end
74
+
75
+ def visit_instance_variable_or_write_node(node)
76
+ highlight :ivar, node.name_loc
77
+ highlight :operator, node.operator_loc
78
+ super
79
+ end
80
+
81
+ def visit_instance_variable_read_node(node)
82
+ highlight :ivar, node.location
83
+ super
84
+ end
85
+
86
+ def visit_constant_read_node(node)
87
+ highlight :constant, node.location
88
+ super
89
+ end
90
+
91
+ def visit_class_node(node)
92
+ highlight :keyword, node.class_keyword_loc
93
+ highlight :keyword, node.end_keyword_loc
94
+ super
95
+ end
96
+
97
+ def visit_call_node(node)
98
+ highlight :call, node.message_loc
99
+ super
100
+ end
101
+
102
+ def visit_string_node(node)
103
+ highlight :string, node.location
104
+ super
105
+ end
106
+
107
+ def visit_interpolated_string_node(node)
108
+ highlight :string, node.opening_loc
109
+ highlight :string, node.closing_loc
110
+ super
111
+ end
112
+
113
+ def visit_symbol_node(node)
114
+ highlight :symbol, node.location
115
+ super
116
+ end
117
+
118
+ def visit_block_node(node)
119
+ if node.opening_loc.length == 1
120
+ highlight :bracket, node.opening_loc
121
+ else
122
+ highlight :keyword, node.opening_loc
123
+ end
124
+
125
+ if node.closing_loc.length == 1
126
+ highlight :bracket, node.closing_loc
127
+ else
128
+ highlight :keyword, node.closing_loc
129
+ end
130
+
131
+ super
132
+ end
133
+
134
+ def visit_case_node(node)
135
+ highlight :keyword, node.case_keyword_loc
136
+ highlight :keyword, node.end_keyword_loc
137
+ super
138
+ end
139
+
140
+ def visit_case_match_node(node)
141
+ highlight :keyword, node.case_keyword_loc
142
+ highlight :keyword, node.end_keyword_loc
143
+ super
144
+ end
145
+
146
+ def visit_module_node(node)
147
+ highlight :keyword, node.module_keyword_loc
148
+ highlight :keyword, node.end_keyword_loc
149
+ super
150
+ end
151
+
152
+ def visit_if_node(node)
153
+ highlight :keyword, node.if_keyword_loc
154
+ highlight :keyword, node.end_keyword_loc
155
+
156
+ if node.then_keyword&.length == 1
157
+ highlight :operator, node.then_keyword_loc
158
+ else
159
+ highlight :keyword, node.then_keyword_loc
160
+ end
161
+
162
+ super
163
+ end
164
+
165
+ def visit_when_node(node)
166
+ highlight :keyword, node.keyword_loc
167
+ super
168
+ end
169
+
170
+ def visit_else_node(node)
171
+ if node.else_keyword.length == 1
172
+ highlight :operator, node.else_keyword_loc
173
+ else
174
+ highlight :keyword, node.else_keyword_loc
175
+ end
176
+
177
+ highlight :keyword, node.end_keyword_loc
178
+
179
+ super
180
+ end
181
+
182
+ def visit_block_argument_node(node)
183
+ highlight :operator, node.operator_loc
184
+ super
185
+ end
186
+
187
+ def visit_and_node(node)
188
+ highlight :operator, node.operator_loc
189
+ super
190
+ end
191
+
192
+ def visit_or_node(node)
193
+ highlight :operator, node.operator_loc
194
+ super
195
+ end
196
+
197
+ def visit_local_variable_or_write_node(node)
198
+ highlight :var, node.name_loc
199
+ highlight :operator, node.operator_loc
200
+ super
201
+ end
202
+
203
+ def visit_local_variable_and_write_node(node)
204
+ highlight :var, node.name_loc
205
+ highlight :operator, node.operator_loc
206
+ super
207
+ end
208
+
209
+ def visit_rescue_node(node)
210
+ highlight :keyword, node.keyword_loc
211
+ highlight :keyword, node.operator_loc
212
+ super
213
+ end
214
+
215
+ def visit_rest_parameter_node(node)
216
+ highlight :operator, node.operator_loc
217
+ super
218
+ end
219
+
220
+ def visit_keyword_rest_parameter_node(node)
221
+ highlight :operator, node.operator_loc
222
+ super
223
+ end
224
+
225
+ def visit_block_parameter_node(node)
226
+ highlight :operator, node.operator_loc
227
+ super
228
+ end
229
+
230
+ def visit_while_node(node)
231
+ highlight :keyword, node.keyword_loc
232
+ highlight :keyword, node.closing_loc
233
+ super
234
+ end
235
+
236
+ def visit_until_node(node)
237
+ highlight :keyword, node.keyword_loc
238
+ highlight :keyword, node.closing_loc
239
+ super
240
+ end
241
+
242
+ def visit_next_node(node)
243
+ highlight :keyword, node.keyword_loc
244
+ super
245
+ end
246
+
247
+ def visit_integer_node(node)
248
+ highlight :numeric, node.location
249
+ super
250
+ end
251
+
252
+ def visit_float_node(node)
253
+ highlight :numeric, node.location
254
+ super
255
+ end
256
+
257
+ def visit_hash_node(node)
258
+ highlight :bracket, node.opening_loc
259
+ highlight :bracket, node.closing_loc
260
+ super
261
+ end
262
+
263
+ def visit_array_node(node)
264
+ highlight :bracket, node.opening_loc
265
+ highlight :bracket, node.closing_loc
266
+ super
267
+ end
268
+
269
+ def visit_assoc_node(node)
270
+ highlight :operator, node.operator_loc
271
+ super
272
+ end
273
+
274
+ def visit_forwarding_parameter_node(node)
275
+ highlight :operator, node.location
276
+ super
277
+ end
278
+
279
+ def visit_forwarding_super_node(node)
280
+ highlight :keyword, node.location
281
+ end
282
+
283
+ def visit_embedded_statements_node(node)
284
+ highlight :bracket, node.opening_loc
285
+ highlight :bracket, node.closing_loc
286
+ super
287
+ end
288
+
289
+ def visit_parentheses_node(node)
290
+ highlight :bracket, node.opening_loc
291
+ highlight :bracket, node.closing_loc
292
+ super
293
+ end
294
+
295
+ def visit_range_node(node)
296
+ highlight :operator, node.operator_loc
297
+ super
298
+ end
299
+
300
+ def visit_in_node(node)
301
+ highlight :keyword, node.in_loc
302
+ highlight :keyword, node.then_loc
303
+ super
304
+ end
305
+
306
+ def visit_array_pattern_node(node)
307
+ highlight :bracket, node.opening_loc
308
+ highlight :bracket, node.closing_loc
309
+ super
310
+ end
311
+
312
+ def visit_alternation_pattern_node(node)
313
+ highlight :operator, node.operator_loc
314
+ super
315
+ end
316
+
317
+ def visit_required_parameter_node(node)
318
+ highlight :var, node.location
319
+ super
320
+ end
321
+
322
+ def visit_optional_parameter_node(node)
323
+ highlight :var, node.name_loc
324
+ highlight :operator, node.operator_loc
325
+ super
326
+ end
327
+
328
+ def visit_capture_pattern_node(node)
329
+ highlight :operator, node.operator_loc
330
+ super
331
+ end
332
+ end
333
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dispersion
4
+ module Formatters
5
+ class ANSI
6
+ Regular = "\e[22m"
7
+ Bold = "\e[1m"
8
+ Faint = "\e[2m"
9
+ Reset = "\e[0m"
10
+ Italic = "\e[3m"
11
+ Underline = "\e[4m"
12
+
13
+ # Basic Colors
14
+ Black = "\e[30m"
15
+ Red = "\e[31m"
16
+ Green = "\e[32m"
17
+ Yellow = "\e[33m"
18
+ Blue = "\e[34m"
19
+ Magenta = "\e[35m"
20
+ Cyan = "\e[36m"
21
+ White = "\e[37m"
22
+
23
+ # Bright Colors
24
+ BrightBlack = "\e[90m"
25
+ BrightRed = "\e[91m"
26
+ BrightGreen = "\e[92m"
27
+ BrightYellow = "\e[93m"
28
+ BrightBlue = "\e[94m"
29
+ BrightMagenta = "\e[95m"
30
+ BrightCyan = "\e[96m"
31
+ BrightWhite = "\e[97m"
32
+
33
+ DEFAULT_ANSI = {
34
+ call: Red,
35
+ string: Yellow,
36
+ keyword: Cyan,
37
+ comment: Italic + Magenta,
38
+ var: "",
39
+ constant: Blue,
40
+ ivar: Blue,
41
+ symbol: Green,
42
+ numeric: Green,
43
+ bracket: "",
44
+ operator: "\e[36m",
45
+ }
46
+
47
+ def initialize(code, annotations:, theme:)
48
+ @code = code
49
+ @annotations = annotations
50
+ @marks = DEFAULT_ANSI
51
+ end
52
+
53
+ def call
54
+ lines = @code.lines
55
+ current_symbol = nil
56
+ buffer = +""
57
+
58
+ lines.each_with_index do |line, line_number|
59
+ cursor = 0
60
+
61
+ @annotations[line_number].each do |(symbol, column)|
62
+ buffer << line[cursor...column]
63
+ cursor = column
64
+
65
+ # Always reset the current symbol
66
+ if current_symbol
67
+ buffer << Reset
68
+ current_symbol = nil
69
+ end
70
+
71
+ unless symbol == :reset
72
+ buffer << @marks.fetch(symbol)
73
+ current_symbol = symbol
74
+ end
75
+ end
76
+
77
+ buffer << line[cursor..]
78
+ end
79
+
80
+ if current_symbol
81
+ buffer << Reset
82
+ current_symbol = nil
83
+ end
84
+
85
+ buffer.gsub(/\t/, " ")
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "erb"
4
+
5
+ module Dispersion
6
+ module Formatters
7
+ class HTML
8
+ def initialize(code, annotations:)
9
+ @code = code
10
+ @annotations = annotations
11
+ end
12
+
13
+ def call
14
+ lines = @code.lines
15
+ current_symbol = nil
16
+ buffer = +""
17
+
18
+ lines.each_with_index do |line, line_number|
19
+ cursor = 0
20
+
21
+ @annotations[line_number].each do |(symbol, column)|
22
+ buffer << ERB::Escape.html_escape(line[cursor...column])
23
+ cursor = column
24
+
25
+ # Always reset the current symbol
26
+ if current_symbol
27
+ buffer << "</span>"
28
+ current_symbol = nil
29
+ end
30
+
31
+ unless symbol == :reset
32
+ buffer << %(<span data-dispersion-symbol="#{symbol.name}">)
33
+ current_symbol = symbol
34
+ end
35
+
36
+ # binding.irb
37
+ end
38
+
39
+ buffer << ERB::Escape.html_escape(line[cursor..])
40
+ end
41
+
42
+ if current_symbol
43
+ buffer << "</span>"
44
+ current_symbol = nil
45
+ end
46
+
47
+ buffer.gsub(/\t/, " ")
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dispersion
4
+ module Formatters
5
+ autoload :HTML, "dispersion/formatters/html"
6
+ autoload :ANSI, "dispersion/formatters/ansi"
7
+ end
8
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dispersion
4
+ class Highlighter
5
+ def initialize(code)
6
+ @code = code
7
+ @annotations = Dispersion::Annotator.call(code)
8
+ freeze
9
+ end
10
+
11
+ def to_ansi(theme: nil)
12
+ Dispersion::Formatters::ANSI.new(@code, annotations: @annotations, theme:).call
13
+ end
14
+
15
+ def to_html
16
+ Dispersion::Formatters::HTML.new(@code, annotations: @annotations).call
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dispersion
4
+ VERSION = "0.1.0"
5
+ end
data/lib/dispersion.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dispersion
4
+ autoload :Annotator, "dispersion/annotator"
5
+ autoload :Annotation, "dispersion/annotation"
6
+ autoload :Formatters, "dispersion/formatters"
7
+ autoload :Highlighter, "dispersion/highlighter"
8
+ autoload :VERSION, "dispersion/version"
9
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dispersion
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel Drapper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: prism
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.18'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.18'
27
+ description:
28
+ email:
29
+ - joel@drapper.me
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - CODE_OF_CONDUCT.md
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - dispersion.gemspec
42
+ - lib/dispersion.rb
43
+ - lib/dispersion/annotation.rb
44
+ - lib/dispersion/annotator.rb
45
+ - lib/dispersion/formatters.rb
46
+ - lib/dispersion/formatters/ansi.rb
47
+ - lib/dispersion/formatters/html.rb
48
+ - lib/dispersion/highlighter.rb
49
+ - lib/dispersion/version.rb
50
+ homepage: https://github.com/joeldrapper/dispersion
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://github.com/joeldrapper/dispersion
55
+ source_code_uri: https://github.com/joeldrapper/dispersion
56
+ changelog_uri: https://github.com/joeldrapper/dispersion/blob/main/CHANGELOG.md
57
+ funding_uri: https://github.com/sponsors/joeldrapper
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.0.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.4.10
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Prism-powered syntax highlighter for Ruby
77
+ test_files: []