rouge 4.6.1 → 5.0.0

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.
Files changed (168) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +11 -4
  3. data/bin/rougify +3 -4
  4. data/lib/rouge/cli.rb +7 -10
  5. data/lib/rouge/demos/abap +30 -4
  6. data/lib/rouge/demos/dylan +8 -0
  7. data/lib/rouge/demos/gjs +23 -0
  8. data/lib/rouge/demos/gts +36 -0
  9. data/lib/rouge/demos/kick_assembler +14 -0
  10. data/lib/rouge/demos/pdf +29 -0
  11. data/lib/rouge/demos/thrift +15 -0
  12. data/lib/rouge/demos/veryl +24 -0
  13. data/lib/rouge/eager.rb +3 -0
  14. data/lib/rouge/formatters/html.rb +8 -1
  15. data/lib/rouge/formatters/html_debug.rb +16 -0
  16. data/lib/rouge/formatters/html_legacy.rb +15 -1
  17. data/lib/rouge/formatters/html_legacy_table.rb +57 -0
  18. data/lib/rouge/formatters/html_line_highlighter.rb +11 -4
  19. data/lib/rouge/formatters/html_line_table.rb +8 -3
  20. data/lib/rouge/formatters/html_linewise.rb +11 -2
  21. data/lib/rouge/formatters/html_pygments.rb +6 -1
  22. data/lib/rouge/formatters/html_table.rb +47 -21
  23. data/lib/rouge/formatters/terminal256.rb +3 -3
  24. data/lib/rouge/formatters/tex.rb +1 -1
  25. data/lib/rouge/guesser.rb +1 -1
  26. data/lib/rouge/guessers/disambiguation.rb +2 -2
  27. data/lib/rouge/guessers/glob_mapping.rb +2 -2
  28. data/lib/rouge/guessers/modeline.rb +2 -2
  29. data/lib/rouge/lexer.rb +40 -21
  30. data/lib/rouge/lexers/abap/builtins.rb +187 -0
  31. data/lib/rouge/lexers/abap.rb +70 -178
  32. data/lib/rouge/lexers/ada.rb +1 -1
  33. data/lib/rouge/lexers/apache/keywords.rb +3 -11
  34. data/lib/rouge/lexers/apache.rb +25 -24
  35. data/lib/rouge/lexers/apex.rb +2 -2
  36. data/lib/rouge/lexers/apiblueprint.rb +2 -2
  37. data/lib/rouge/lexers/bicep.rb +2 -2
  38. data/lib/rouge/lexers/biml.rb +36 -9
  39. data/lib/rouge/lexers/brightscript.rb +3 -4
  40. data/lib/rouge/lexers/c.rb +9 -5
  41. data/lib/rouge/lexers/cobol.rb +1 -1
  42. data/lib/rouge/lexers/console.rb +11 -5
  43. data/lib/rouge/lexers/cpp.rb +10 -12
  44. data/lib/rouge/lexers/crystal.rb +1 -1
  45. data/lib/rouge/lexers/css.rb +21 -2
  46. data/lib/rouge/lexers/cuda.rb +4 -4
  47. data/lib/rouge/lexers/cython.rb +26 -50
  48. data/lib/rouge/lexers/dafny.rb +1 -1
  49. data/lib/rouge/lexers/dart.rb +2 -0
  50. data/lib/rouge/lexers/datastudio.rb +1 -1
  51. data/lib/rouge/lexers/digdag.rb +2 -3
  52. data/lib/rouge/lexers/dylan.rb +109 -0
  53. data/lib/rouge/lexers/ecl.rb +3 -3
  54. data/lib/rouge/lexers/elixir.rb +14 -9
  55. data/lib/rouge/lexers/escape.rb +1 -1
  56. data/lib/rouge/lexers/factor.rb +1 -1
  57. data/lib/rouge/lexers/freefem.rb +2 -2
  58. data/lib/rouge/lexers/ghc_cmm.rb +1 -1
  59. data/lib/rouge/lexers/ghc_core.rb +1 -1
  60. data/lib/rouge/lexers/gherkin/keywords.rb +10 -6
  61. data/lib/rouge/lexers/gherkin.rb +28 -26
  62. data/lib/rouge/lexers/gjs.rb +39 -0
  63. data/lib/rouge/lexers/glsl/builtins.rb +17 -0
  64. data/lib/rouge/lexers/glsl.rb +50 -113
  65. data/lib/rouge/lexers/go.rb +8 -6
  66. data/lib/rouge/lexers/gradle.rb +2 -2
  67. data/lib/rouge/lexers/groovy.rb +6 -0
  68. data/lib/rouge/lexers/gts.rb +39 -0
  69. data/lib/rouge/lexers/hack.rb +4 -6
  70. data/lib/rouge/lexers/haxe.rb +2 -2
  71. data/lib/rouge/lexers/hlsl.rb +6 -6
  72. data/lib/rouge/lexers/hocon.rb +2 -2
  73. data/lib/rouge/lexers/hql.rb +15 -19
  74. data/lib/rouge/lexers/idris.rb +1 -1
  75. data/lib/rouge/lexers/igorpro/builtins.rb +1530 -0
  76. data/lib/rouge/lexers/igorpro.rb +49 -520
  77. data/lib/rouge/lexers/irb.rb +3 -3
  78. data/lib/rouge/lexers/isbl.rb +7 -43
  79. data/lib/rouge/lexers/j.rb +1 -1
  80. data/lib/rouge/lexers/json5.rb +2 -2
  81. data/lib/rouge/lexers/json_doc.rb +2 -2
  82. data/lib/rouge/lexers/jsp.rb +119 -119
  83. data/lib/rouge/lexers/jsx.rb +2 -2
  84. data/lib/rouge/lexers/kick_assembler.rb +100 -0
  85. data/lib/rouge/lexers/kotlin.rb +5 -0
  86. data/lib/rouge/lexers/lasso/keywords.rb +6 -6
  87. data/lib/rouge/lexers/lasso.rb +9 -11
  88. data/lib/rouge/lexers/liquid.rb +5 -6
  89. data/lib/rouge/lexers/llvm/keywords.rb +3 -12
  90. data/lib/rouge/lexers/llvm.rb +5 -16
  91. data/lib/rouge/lexers/lua/keywords.rb +11 -13
  92. data/lib/rouge/lexers/lua.rb +8 -7
  93. data/lib/rouge/lexers/lutin.rb +5 -4
  94. data/lib/rouge/lexers/m68k.rb +3 -3
  95. data/lib/rouge/lexers/mason.rb +110 -110
  96. data/lib/rouge/lexers/mathematica/keywords.rb +1 -3
  97. data/lib/rouge/lexers/mathematica.rb +6 -7
  98. data/lib/rouge/lexers/matlab/keywords.rb +2 -2
  99. data/lib/rouge/lexers/matlab.rb +5 -5
  100. data/lib/rouge/lexers/meson.rb +1 -1
  101. data/lib/rouge/lexers/mojo.rb +6 -2
  102. data/lib/rouge/lexers/moonscript.rb +19 -17
  103. data/lib/rouge/lexers/nesasm.rb +6 -6
  104. data/lib/rouge/lexers/nial.rb +7 -8
  105. data/lib/rouge/lexers/nim.rb +3 -3
  106. data/lib/rouge/lexers/nix.rb +10 -8
  107. data/lib/rouge/lexers/objective_c.rb +3 -3
  108. data/lib/rouge/lexers/objective_cpp.rb +3 -2
  109. data/lib/rouge/lexers/ocaml.rb +2 -2
  110. data/lib/rouge/lexers/openedge.rb +9 -9
  111. data/lib/rouge/lexers/pdf.rb +109 -0
  112. data/lib/rouge/lexers/php/keywords.rb +136 -188
  113. data/lib/rouge/lexers/php.rb +100 -11
  114. data/lib/rouge/lexers/plsql.rb +8 -9
  115. data/lib/rouge/lexers/postscript.rb +1 -1
  116. data/lib/rouge/lexers/powershell.rb +1 -1
  117. data/lib/rouge/lexers/python.rb +111 -66
  118. data/lib/rouge/lexers/qml.rb +2 -2
  119. data/lib/rouge/lexers/reasonml.rb +2 -2
  120. data/lib/rouge/lexers/rego.rb +53 -53
  121. data/lib/rouge/lexers/rescript.rb +2 -2
  122. data/lib/rouge/lexers/robot_framework.rb +12 -12
  123. data/lib/rouge/lexers/{coq.rb → rocq.rb} +15 -6
  124. data/lib/rouge/lexers/ruby.rb +33 -7
  125. data/lib/rouge/lexers/rust.rb +1 -0
  126. data/lib/rouge/lexers/sas.rb +164 -165
  127. data/lib/rouge/lexers/sass/common.rb +1 -1
  128. data/lib/rouge/lexers/sass.rb +2 -2
  129. data/lib/rouge/lexers/scss.rb +2 -2
  130. data/lib/rouge/lexers/sed.rb +1 -1
  131. data/lib/rouge/lexers/shell.rb +5 -5
  132. data/lib/rouge/lexers/slice.rb +2 -2
  133. data/lib/rouge/lexers/sparql.rb +1 -1
  134. data/lib/rouge/lexers/sqf/keywords.rb +5 -3
  135. data/lib/rouge/lexers/sqf.rb +3 -4
  136. data/lib/rouge/lexers/stan.rb +9 -15
  137. data/lib/rouge/lexers/svelte.rb +2 -2
  138. data/lib/rouge/lexers/swift.rb +2 -2
  139. data/lib/rouge/lexers/tcl.rb +1 -1
  140. data/lib/rouge/lexers/terraform.rb +3 -3
  141. data/lib/rouge/lexers/thrift.rb +120 -0
  142. data/lib/rouge/lexers/tsx.rb +3 -3
  143. data/lib/rouge/lexers/ttcn3.rb +1 -1
  144. data/lib/rouge/lexers/twig.rb +2 -2
  145. data/lib/rouge/lexers/typescript.rb +3 -3
  146. data/lib/rouge/lexers/varnish.rb +8 -8
  147. data/lib/rouge/lexers/veryl.rb +109 -0
  148. data/lib/rouge/lexers/viml/keywords.rb +5 -7
  149. data/lib/rouge/lexers/viml.rb +8 -10
  150. data/lib/rouge/lexers/vue.rb +2 -2
  151. data/lib/rouge/lexers/xquery.rb +2 -1
  152. data/lib/rouge/lexers/yaml.rb +13 -1
  153. data/lib/rouge/lexers/yang.rb +42 -74
  154. data/lib/rouge/lexers/zig.rb +10 -10
  155. data/lib/rouge/plugins/redcarpet.rb +3 -2
  156. data/lib/rouge/regex_lexer.rb +25 -14
  157. data/lib/rouge/themes/gruvbox.rb +3 -0
  158. data/lib/rouge/token.rb +1 -1
  159. data/lib/rouge/util.rb +1 -1
  160. data/lib/rouge/version.rb +1 -1
  161. data/lib/rouge.rb +70 -59
  162. data/lib/rubocop/cop/rouge/no_building_alternation_pattern_in_regexp.rb +71 -0
  163. data/lib/rubocop/cop/rouge/no_huge_collections.rb +37 -0
  164. data/rouge.gemspec +8 -5
  165. metadata +44 -9
  166. data/lib/rouge/lexers/isbl/builtins.rb +0 -17
  167. data/lib/rouge/lexers/matlab/builtins.rb +0 -11
  168. /data/lib/rouge/demos/{coq → rocq} +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 83340a3ee45685522d7b91daf3d85c87721430d3606ab761353099941f272f4e
4
- data.tar.gz: 112aa358b29944ddbba5d837ae32bcd833a7c5c9d9b4100a952a265dc9c5459c
3
+ metadata.gz: 9123349dc8d157c07642dd422b2ce6589c42728fcc9a204255b55c2cdac11a41
4
+ data.tar.gz: b077e0041bb215f3e3c33799e333c4983df1d98d35ecf0606e82563de05c2c0b
5
5
  SHA512:
6
- metadata.gz: d266d81096d98c4cbd51d0cc79edef8887a40465e749bfc51d32ae0a493b0be23e090234336a8accfdfe45d48e4a25d4bd50391f369d8901c5a1ec946071d604
7
- data.tar.gz: 011ff0045c3de53ca6e1647651d3fe8cd722a57656d3d08ca4559a1dfb40b3940162c6502ff71ead6ebe0ff95a60e8924d2d50ee5c0b144bff344a6392563f7c
6
+ metadata.gz: 3b94cebfc00d70f126ae1ee7675c123d1d2f6bbbdaba02b659c1f864392f457f7e934117f43a1f17691eefa1c3b2b160f2c2e43f368bcf39737a766d98d87e7e
7
+ data.tar.gz: d21221f27e9a98c1698a870fde934dc914e6cb7fea60ad782e2f3bcfa795ea7fc6eac2034193266e73ec8194f53f6c69d4a812467612536417bfb60bf39aec07
data/Gemfile CHANGED
@@ -23,8 +23,10 @@ group :development do
23
23
  gem 'git'
24
24
  gem 'yard'
25
25
 
26
- gem 'rubocop', '~> 1.0', '<= 1.11'
27
- gem 'rubocop-performance'
26
+ gem 'rubocop'
27
+ gem 'rubocop-performance', '~> 1.26'
28
+ gem 'rubocop-minitest', '~> 0.38'
29
+ gem 'rubocop-rake', '~> 0.7'
28
30
 
29
31
  # docs
30
32
  gem 'github-markup'
@@ -32,10 +34,15 @@ group :development do
32
34
  # for visual tests
33
35
  gem 'sinatra'
34
36
 
35
- # Ruby 3 no longer ships with a web server
36
- gem 'puma' if RUBY_VERSION >= '3'
37
+ gem 'puma'
37
38
  gem 'shotgun'
38
39
 
39
40
  gem "mutex_m" if RUBY_VERSION >= '3.4'
40
41
  gem "base64" if RUBY_VERSION >= '3.4'
42
+
43
+ gem 'ostruct'
44
+ gem 'reline'
45
+ gem 'nokogiri'
46
+
47
+ gem 'svn-downloader'
41
48
  end
data/bin/rougify CHANGED
@@ -1,10 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'pathname'
5
- ROOT_DIR = Pathname.new(__FILE__).dirname.parent
6
- Kernel::load ROOT_DIR.join('lib/rouge.rb')
7
- Kernel::load ROOT_DIR.join('lib/rouge/cli.rb')
4
+ require_relative "../lib/rouge"
5
+ require_relative "../lib/rouge/cli"
6
+
8
7
  Signal.trap('PIPE', 'SYSTEM_DEFAULT') if Signal.list.include? 'PIPE'
9
8
 
10
9
  begin
data/lib/rouge/cli.rb CHANGED
@@ -5,7 +5,6 @@
5
5
  # to use this module, require 'rouge/cli'.
6
6
 
7
7
  require 'rbconfig'
8
- require 'uri'
9
8
 
10
9
  module Rouge
11
10
  class FileReader
@@ -160,7 +159,7 @@ module Rouge
160
159
  end
161
160
 
162
161
  def run
163
- @mode.doc.each(&method(:puts))
162
+ @mode.doc.each { |line| puts line }
164
163
  end
165
164
  end
166
165
 
@@ -213,7 +212,7 @@ module Rouge
213
212
  # and we provide explicit opt-in and opt-out with $COLORTERM
214
213
  def self.supports_truecolor?
215
214
  return true if %w(24bit truecolor).include?(ENV['COLORTERM'])
216
- return false if ENV['COLORTERM'] && ENV['COLORTERM'] =~ /256/
215
+ return false if ENV['COLORTERM'] && ENV['COLORTERM'].include?('256')
217
216
 
218
217
  if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
219
218
  ENV['ConEmuANSI'] == 'ON' && !ENV['ANSICON']
@@ -345,10 +344,10 @@ module Rouge
345
344
 
346
345
  def run
347
346
  Formatter.enable_escape! if @escape
348
- formatter.format(lexer.lex(input), &method(:print))
347
+ formatter.format(lexer.lex(input)) { |piece| print piece }
349
348
  end
350
349
 
351
- private_class_method def self.parse_cgi(str)
350
+ private_class_method def self.parse_cgi(str)
352
351
  pairs = URI.decode_www_form(str).map { |k, v| [k.to_sym, v] }
353
352
  Hash[pairs]
354
353
  end
@@ -440,7 +439,7 @@ module Rouge
440
439
  end
441
440
 
442
441
  def run
443
- @theme.render(&method(:puts))
442
+ @theme.render { |line| puts line }
444
443
  end
445
444
  end
446
445
 
@@ -511,13 +510,11 @@ module Rouge
511
510
  end
512
511
 
513
512
 
514
- private_class_method def self.normalize_syntax(argv)
513
+ private_class_method def self.normalize_syntax(argv)
515
514
  out = []
516
515
  argv.each do |arg|
517
516
  case arg
518
- when /^(--\w+)=(.*)$/
519
- out << $1 << $2
520
- when /^(-\w)(.+)$/
517
+ when /^(--\w+)=(.*)$/, /^(-\w)(.+)$/
521
518
  out << $1 << $2
522
519
  else
523
520
  out << arg
data/lib/rouge/demos/abap CHANGED
@@ -1,6 +1,32 @@
1
- lo_obj ?= lo_obj->do_nothing( 'Char' && ` String` ).
1
+ * Create a new object
2
+ DATA(lo_obj) = new zcl_obj_factory(
3
+ EXPORTING
4
+ input = |some nice string|
5
+ )
6
+ DATA(lo_subobj) ?= lo_obj->do_nothing( 'Char' && ` String` ).
2
7
 
3
- SELECT SINGLE * FROM mara INTO ls_mara WHERE matkl EQ '1324'.
4
- LOOP AT lt_mara ASSIGNING <mara>.
5
- CHECK <mara>-mtart EQ '0001'.
8
+ " fetch material data
9
+ SELECT SINGLE matnr, matkl, mtart, maktx
10
+ FROM mara
11
+ INNER JOIN makt
12
+ ON makt~matnr EQ mara~matnr
13
+ INTO @data(ls_mara)
14
+ WHERE matkl EQ '1324'
15
+ AND spras EQ @sy-langu.
16
+
17
+ LOOP AT lt_mara ASSIGNING FIELD-SYMBOL(<mara>).
18
+ CHECK <mara>-mtart = '0001'.
6
19
  ENDLOOP.
20
+
21
+ CALL FUNCTION 'DO_NOTHING'
22
+ EXPORTING
23
+ input = 'input'
24
+ IMPORTING
25
+ output = 'output'
26
+ CHANGING
27
+ to_change = lv_some_variable
28
+ TABLES
29
+ table_to_change = lt_some_table
30
+ EXCEPTIONS
31
+ ERROR = 1
32
+ OTHERS = 2.
@@ -0,0 +1,8 @@
1
+ // Various keywords and constants
2
+ define method demo(x :: <integer>, #key y = "demo") => ()
3
+ let z = #xFEE8 + #o277;
4
+ let w = '\n';
5
+ format-out("%d %s %d %c", x + 100, y, z, w);
6
+ end method;
7
+
8
+ demo(99, y: "hello");
@@ -0,0 +1,23 @@
1
+ import { LinkTo } from '@ember/routing';
2
+ import { local } from 'embroider-css-modules';
3
+
4
+ import styles from './navigation-menu.css';
5
+
6
+ const NavigationMenu = <template>
7
+ <nav aria-label={{@name}} data-test-nav={{@name}}>
8
+ <ul class={{styles.list}}>
9
+ {{#each @menuItems as |menuItem|}}
10
+ <li>
11
+ <LinkTo
12
+ class={{local styles "link"}}
13
+ data-test-link={{menuItem.label}}
14
+ >
15
+ {{menuItem.label}}
16
+ </LinkTo>
17
+ </li>
18
+ {{/each}}
19
+ </ul>
20
+ </nav>
21
+ </template>;
22
+
23
+ export default NavigationMenu;
@@ -0,0 +1,36 @@
1
+ import type { TOC } from '@ember/component/template-only';
2
+ import { LinkTo } from '@ember/routing';
3
+ import { local } from 'embroider-css-modules';
4
+
5
+ import styles from './navigation-menu.css';
6
+
7
+ type MenuItem = {
8
+ label: string;
9
+ route: string;
10
+ };
11
+
12
+ interface NavigationMenuSignature {
13
+ Args: {
14
+ menuItems: MenuItem[];
15
+ name?: string;
16
+ };
17
+ }
18
+
19
+ const NavigationMenu = <template>
20
+ <nav aria-label={{@name}} data-test-nav={{@name}}>
21
+ <ul class={{styles.list}}>
22
+ {{#each @menuItems as |menuItem|}}
23
+ <li>
24
+ <LinkTo
25
+ class={{local styles "link"}}
26
+ data-test-link={{menuItem.label}}
27
+ >
28
+ {{menuItem.label}}
29
+ </LinkTo>
30
+ </li>
31
+ {{/each}}
32
+ </ul>
33
+ </nav>
34
+ </template> satisfies TOC<NavigationMenuSignature>;
35
+
36
+ export default NavigationMenu;
@@ -0,0 +1,14 @@
1
+ BasicUpstart2(start)
2
+
3
+ .const GET_CHAR = $ffe4
4
+
5
+ start:
6
+ jsr get_char
7
+ rts
8
+
9
+ // Routine
10
+ get_char: {
11
+ jsr GET_CHAR
12
+ beq get_char
13
+ rts
14
+ }
@@ -0,0 +1,29 @@
1
+ %PDF-1.6
2
+ %©©©©
3
+
4
+ 1 0 obj<</Type/Catalog/Pages 2 0 R/StructTreeRoot null/MarkInfo<</Marked false>>>>
5
+ endobj
6
+ 2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>
7
+ endobj
8
+ 3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[.0 0 200 200]/Contents 4 0 R/Resources<<>>>>
9
+ endobj
10
+ 4 0 obj<</Length 60>>
11
+ stream
12
+ +8 w 1 j
13
+ 1.0 0 0 rg
14
+ 0 0 1 RG
15
+ 10 10 180 180 re B
16
+ endstream
17
+ endobj
18
+ xref
19
+ 0 5
20
+ 0000000000 65535 f
21
+ 0000000021 00000 n
22
+ 0000000113 00000 n
23
+ 0000000165 00000 n
24
+ 0000000261 00000 n
25
+ trailer
26
+ <</Root 1 0 R/Size 5/ID[<18D6B641245C03F28E67D93AD879D6EC><18D6B641245C03F28E67D93AD879D6EC>]>>
27
+ startxref
28
+ 371
29
+ %%EOF
@@ -0,0 +1,15 @@
1
+ namespace * tutorial
2
+
3
+ include "shared.thrift"
4
+
5
+ struct Work {
6
+ 1: i32 num1 = 0,
7
+ 2: i32 num2,
8
+ 3: Operation op,
9
+ 4: optional string comment,
10
+ }
11
+
12
+ service Calculator extends shared.SharedService {
13
+ i32 add(1: i32 num1, 2: i32 num2),
14
+ oneway void zip()
15
+ }
@@ -0,0 +1,24 @@
1
+ /// Module A
2
+ module ModuleA #(
3
+ param ParamA: u32 = 10,
4
+ ) (
5
+ i_clk : input clock ,
6
+ i_rst : input reset ,
7
+ i_sel : input logic ,
8
+ i_data: input logic<ParamA> [2],
9
+ o_data: output logic<ParamA> ,
10
+ ) {
11
+ var r_data: logic<ParamA>;
12
+
13
+ always_ff {
14
+ if_reset {
15
+ r_data = 0;
16
+ } else if i_sel {
17
+ r_data = i_data[0];
18
+ } else {
19
+ r_data = i_data[1];
20
+ }
21
+ }
22
+
23
+ assign o_data = r_data;
24
+ }
@@ -0,0 +1,3 @@
1
+ require_relative "../rouge"
2
+
3
+ Rouge.eager_load!
@@ -9,9 +9,16 @@ module Rouge
9
9
  '&' => '&amp;',
10
10
  '<' => '&lt;',
11
11
  '>' => '&gt;',
12
+ "\r" => "",
12
13
  }.freeze
13
14
 
14
- ESCAPE_REGEX = /[&<>]/.freeze
15
+ ESCAPE_REGEX = /[&<>\r]/.freeze
16
+
17
+ def self.assert_html_formatter!(formatter)
18
+ return formatter if formatter.respond_to?(:span)
19
+
20
+ raise ArgumentError.new("Expected an instance of Rouge::Formatters::HTML, got #{formatter.class}. Try HTML, HTMLDebug, or HTMLInline.")
21
+ end
15
22
 
16
23
  tag 'html'
17
24
 
@@ -0,0 +1,16 @@
1
+ module Rouge
2
+ module Formatters
3
+ class HTMLDebug < HTML
4
+ tag 'html_debug'
5
+
6
+ def safe_span(tok, safe_val)
7
+ safer_val = safe_val.gsub('"', '&quot;')
8
+
9
+ title = "#{tok.qualname}(#{safer_val})"
10
+ shortname = tok.shortname
11
+
12
+ %(<span title="#{title}" class="#{shortname}">#{safe_val}</span>)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,6 +1,11 @@
1
1
  # -*- coding: utf-8 -*- #
2
2
  # frozen_string_literal: true
3
3
 
4
+ # [jneen] This is an example implementation only. You may use it as-is, but please do
5
+ # not submit patches that alter the behaviour or options of this formatter for the
6
+ # convenience of your application. You are highly encouraged to write your own
7
+ # formatter for your application instead.
8
+
4
9
  module Rouge
5
10
  module Formatters
6
11
  # Transforms a token stream into HTML output.
@@ -22,6 +27,7 @@ module Rouge
22
27
  # Content will be wrapped in a tag (`div` if tableized, `pre` if
23
28
  # not) with the given `:css_class` unless `:wrap` is set to `false`.
24
29
  def initialize(opts={})
30
+ warn '[DEPRECATED] Rouge::Formatters::HTMLLegacy is deprecated and will be removed soon. Please use one of the other formatters, or write your own.'
25
31
  @formatter = opts[:inline_theme] ? HTMLInline.new(opts[:inline_theme])
26
32
  : HTML.new
27
33
 
@@ -29,13 +35,21 @@ module Rouge
29
35
  @formatter = HTMLTable.new(@formatter, opts) if opts[:line_numbers]
30
36
 
31
37
  if opts.fetch(:wrap, true)
32
- @formatter = HTMLPygments.new(@formatter, opts.fetch(:css_class, 'codehilite'))
38
+ @pygments_wrap = opts.fetch(:css_class, 'codehilite')
33
39
  end
34
40
  end
35
41
 
36
42
  # @yield the html output.
37
43
  def stream(tokens, &b)
44
+ if @pygments_wrap
45
+ yield %(<div class="highlight"><pre class="#{@pygments_wrap}"><code>)
46
+ end
47
+
38
48
  @formatter.stream(tokens, &b)
49
+
50
+ if @pygments_wrap
51
+ yield "</code></pre></div>"
52
+ end
39
53
  end
40
54
  end
41
55
  end
@@ -0,0 +1,57 @@
1
+ # -*- coding: utf-8 -*- #
2
+ # frozen_string_literal: true
3
+
4
+ # [jneen] This is an example implementation only. You may use it as-is, but please do
5
+ # not submit patches that alter the behaviour or options of this formatter for the
6
+ # convenience of your application. You are highly encouraged to write your own
7
+ # formatter for your application instead.
8
+
9
+ module Rouge
10
+ module Formatters
11
+ class HTMLLegacyTable < Formatter
12
+ def initialize(inner, opts={})
13
+ warn '[DEPRECATED] using a wrapped or line-managed delegator for HTMLTable is deprecated, and will be removed soon. Please use HTML, HTMLInline, or HTMLDebug, or write your own formatter.'
14
+
15
+ @inner = inner
16
+ @start_line = opts.fetch(:start_line, 1)
17
+ @line_format = opts.fetch(:line_format, '%i')
18
+ @table_class = opts.fetch(:table_class, 'rouge-table')
19
+ @gutter_class = opts.fetch(:gutter_class, 'rouge-gutter')
20
+ @code_class = opts.fetch(:code_class, 'rouge-code')
21
+ end
22
+
23
+ def style(scope)
24
+ yield "#{scope} .#{@table_class} { border-spacing: 0 }"
25
+ yield "#{scope} .#{@gutter_class} { text-align: right }"
26
+ end
27
+
28
+ def stream(tokens, &b)
29
+ last_val = nil
30
+ num_lines = tokens.reduce(0) {|count, (_, val)| count + (last_val = val).count("\n") }
31
+ formatted = @inner.format(tokens)
32
+
33
+ unless last_val && last_val.end_with?("\n")
34
+ num_lines += 1
35
+ formatted << "\n" unless formatted.end_with?("\n")
36
+ end
37
+
38
+ # generate a string of newline-separated line numbers for the gutter
39
+ formatted_line_numbers = (@start_line..(@start_line + num_lines - 1)).map do |i|
40
+ sprintf(@line_format, i)
41
+ end.join("\n") << "\n"
42
+
43
+ buffer = [%(<table class="#@table_class"><tbody><tr>)]
44
+ # the "gl" class applies the style for Generic.Lineno
45
+ buffer << %(<td class="#@gutter_class gl" aria-hidden="true">)
46
+ buffer << %(<pre class="lineno">#{formatted_line_numbers}</pre>)
47
+ buffer << '</td>'
48
+ buffer << %(<td class="#@code_class"><pre><code>)
49
+ buffer << formatted
50
+ buffer << '</code></pre></td>'
51
+ buffer << '</tr></tbody></table>'
52
+
53
+ yield buffer.join
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,22 +1,29 @@
1
1
  # -*- coding: utf-8 -*- #
2
2
  # frozen_string_literal: true
3
3
 
4
+ # [jneen] This is an example implementation only. You may use it as-is, but please do
5
+ # not submit patches that alter the behaviour or options of this formatter for the
6
+ # convenience of your application. You are highly encouraged to write your own
7
+ # formatter for your application instead.
8
+
4
9
  module Rouge
5
10
  module Formatters
6
11
  class HTMLLineHighlighter < Formatter
7
12
  tag 'html_line_highlighter'
8
13
 
9
14
  def initialize(delegate, opts = {})
10
- @delegate = delegate
15
+ @delegate = HTML.assert_html_formatter!(delegate)
11
16
  @highlight_line_class = opts.fetch(:highlight_line_class, 'hll')
12
17
  @highlight_lines = opts[:highlight_lines] || []
13
18
  end
14
19
 
15
20
  def stream(tokens)
16
21
  token_lines(tokens).with_index(1) do |line_tokens, lineno|
17
- line = %(#{@delegate.format(line_tokens)}\n)
18
- line = %(<span class="#{@highlight_line_class}">#{line}</span>) if @highlight_lines.include? lineno
19
- yield line
22
+ should_highlight = @highlight_lines.include?(lineno)
23
+ yield %(<span class=#{@highlight_line_class.inspect}>) if should_highlight
24
+ line_tokens.each { |tok, val| yield @delegate.span(tok, val) }
25
+ yield "\n"
26
+ yield %(</span>) if should_highlight
20
27
  end
21
28
  end
22
29
  end
@@ -1,6 +1,11 @@
1
1
  # -*- coding: utf-8 -*- #
2
2
  # frozen_string_literal: true
3
3
 
4
+ # [jneen] This is an example implementation only. You may use it as-is, but please do
5
+ # not submit patches that alter the behaviour or options of this formatter for the
6
+ # convenience of your application. You are highly encouraged to write your own
7
+ # formatter for your application instead.
8
+
4
9
  module Rouge
5
10
  module Formatters
6
11
  class HTMLLineTable < Formatter
@@ -22,7 +27,7 @@ module Rouge
22
27
  # @option opts [String] :code_class Class name for rendered code cell.
23
28
  # Defaults to `"rouge-code"`.
24
29
  def initialize(formatter, opts={})
25
- @formatter = formatter
30
+ @formatter = HTML.assert_html_formatter!(formatter)
26
31
  @start_line = opts.fetch :start_line, 1
27
32
  @table_class = opts.fetch :table_class, 'rouge-line-table'
28
33
  @gutter_class = opts.fetch :gutter_class, 'rouge-gutter'
@@ -39,9 +44,9 @@ module Rouge
39
44
  buffer << %(style="-moz-user-select: none;-ms-user-select: none;)
40
45
  buffer << %(-webkit-user-select: none;user-select: none;">)
41
46
  buffer << %(<pre>#{lineno}</pre></td>)
42
- buffer << %(<td class="#@code_class"><pre>)
47
+ buffer << %(<td class="#@code_class"><pre><code>)
43
48
  @formatter.stream(line_tokens) { |formatted| buffer << formatted }
44
- buffer << "\n</pre></td></tr>"
49
+ buffer << "\n</code></pre></td></tr>"
45
50
  end
46
51
  buffer << %(</tbody></table>)
47
52
  yield buffer.join
@@ -1,11 +1,18 @@
1
1
  # -*- coding: utf-8 -*- #
2
2
  # frozen_string_literal: true
3
3
 
4
+ # [jneen] This is an example implementation only. You may use it as-is, but please do
5
+ # not submit patches that alter the behaviour or options of this formatter for the
6
+ # convenience of your application. You are highly encouraged to write your own
7
+ # formatter for your application instead.
8
+
4
9
  module Rouge
5
10
  module Formatters
6
11
  class HTMLLinewise < Formatter
12
+ tag 'html_linewise'
13
+
7
14
  def initialize(formatter, opts={})
8
- @formatter = formatter
15
+ @formatter = HTML.assert_html_formatter!(formatter)
9
16
  @tag_name = opts.fetch(:tag_name, 'div')
10
17
  @class_format = opts.fetch(:class, 'line-%i')
11
18
  end
@@ -13,7 +20,9 @@ module Rouge
13
20
  def stream(tokens, &b)
14
21
  token_lines(tokens).with_index(1) do |line_tokens, lineno|
15
22
  yield %(<#{@tag_name} class="#{sprintf @class_format, lineno}">)
16
- @formatter.stream(line_tokens) {|formatted| yield formatted }
23
+ line_tokens.each do |tok, val|
24
+ yield @formatter.span(tok, val)
25
+ end
17
26
  yield %(\n</#{@tag_name}>)
18
27
  end
19
28
  end
@@ -1,10 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # [jneen] This is an example implementation only. You may use it as-is, but please do
4
+ # not submit patches that alter the behaviour or options of this formatter for the
5
+ # convenience of your application. You are highly encouraged to write your own
6
+ # formatter for your application instead.
7
+
3
8
  module Rouge
4
9
  module Formatters
5
10
  class HTMLPygments < Formatter
6
11
  def initialize(inner, css_class='codehilite')
7
- @inner = inner
12
+ @inner = HTML.assert_html_formatter!(inner)
8
13
  @css_class = css_class
9
14
  end
10
15