rouge 3.1.0 → 3.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (318) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile +17 -3
  3. data/bin/rougify +1 -0
  4. data/lib/rouge.rb +67 -42
  5. data/lib/rouge/cli.rb +87 -18
  6. data/lib/rouge/demos/ada +26 -0
  7. data/lib/rouge/demos/apex +9 -0
  8. data/lib/rouge/demos/armasm +12 -0
  9. data/lib/rouge/demos/batchfile +3 -0
  10. data/lib/rouge/demos/bbcbasic +6 -0
  11. data/lib/rouge/demos/bpf +7 -0
  12. data/lib/rouge/demos/brainfuck +5 -0
  13. data/lib/rouge/demos/clean +6 -0
  14. data/lib/rouge/demos/cmhg +8 -0
  15. data/lib/rouge/demos/crystal +45 -0
  16. data/lib/rouge/demos/csvs +8 -0
  17. data/lib/rouge/demos/cuda +11 -0
  18. data/lib/rouge/demos/cypher +5 -0
  19. data/lib/rouge/demos/cython +6 -0
  20. data/lib/rouge/demos/datastudio +21 -0
  21. data/lib/rouge/demos/ecl +18 -0
  22. data/lib/rouge/demos/eex +1 -0
  23. data/lib/rouge/demos/epp +4 -0
  24. data/lib/rouge/demos/escape +3 -0
  25. data/lib/rouge/demos/freefem +16 -0
  26. data/lib/rouge/demos/gdscript +18 -0
  27. data/lib/rouge/demos/ghc-cmm +23 -0
  28. data/lib/rouge/demos/ghc-core +26 -0
  29. data/lib/rouge/demos/haxe +5 -0
  30. data/lib/rouge/demos/hcl +7 -0
  31. data/lib/rouge/demos/hocon +8 -0
  32. data/lib/rouge/demos/hql +5 -0
  33. data/lib/rouge/demos/isbl +4 -0
  34. data/lib/rouge/demos/jsl +3 -0
  35. data/lib/rouge/demos/jsp +29 -0
  36. data/lib/rouge/demos/liquid +0 -1
  37. data/lib/rouge/demos/lustre +6 -0
  38. data/lib/rouge/demos/lutin +18 -0
  39. data/lib/rouge/demos/m68k +16 -0
  40. data/lib/rouge/demos/magik +6 -0
  41. data/lib/rouge/demos/mason +22 -0
  42. data/lib/rouge/demos/mathematica +8 -0
  43. data/lib/rouge/demos/minizinc +23 -0
  44. data/lib/rouge/demos/msgtrans +4 -0
  45. data/lib/rouge/demos/nasm +6 -6
  46. data/lib/rouge/demos/nesasm +11 -0
  47. data/lib/rouge/demos/objective_cpp +17 -0
  48. data/lib/rouge/demos/openedge +4 -0
  49. data/lib/rouge/demos/opentype_feature_file +6 -0
  50. data/lib/rouge/demos/plist +1 -132
  51. data/lib/rouge/demos/powershell +12 -48
  52. data/lib/rouge/demos/q +6 -0
  53. data/lib/rouge/demos/reasonml +12 -0
  54. data/lib/rouge/demos/rego +8 -0
  55. data/lib/rouge/demos/robot_framework +27 -0
  56. data/lib/rouge/demos/sas +13 -0
  57. data/lib/rouge/demos/slice +10 -0
  58. data/lib/rouge/demos/solidity +13 -0
  59. data/lib/rouge/demos/sparql +6 -0
  60. data/lib/rouge/demos/sqf +14 -0
  61. data/lib/rouge/demos/supercollider +11 -0
  62. data/lib/rouge/demos/terraform +16 -0
  63. data/lib/rouge/demos/ttcn3 +6 -0
  64. data/lib/rouge/demos/vcl +12 -0
  65. data/lib/rouge/demos/xojo +14 -0
  66. data/lib/rouge/demos/xpath +2 -0
  67. data/lib/rouge/demos/xquery +22 -0
  68. data/lib/rouge/demos/yang +17 -0
  69. data/lib/rouge/formatter.rb +39 -2
  70. data/lib/rouge/formatters/html.rb +21 -2
  71. data/lib/rouge/formatters/html_inline.rb +1 -0
  72. data/lib/rouge/formatters/html_legacy.rb +1 -0
  73. data/lib/rouge/formatters/html_line_table.rb +53 -0
  74. data/lib/rouge/formatters/html_linewise.rb +7 -11
  75. data/lib/rouge/formatters/html_pygments.rb +2 -0
  76. data/lib/rouge/formatters/html_table.rb +21 -31
  77. data/lib/rouge/formatters/null.rb +1 -0
  78. data/lib/rouge/formatters/terminal256.rb +23 -5
  79. data/lib/rouge/formatters/terminal_truecolor.rb +37 -0
  80. data/lib/rouge/formatters/tex.rb +92 -0
  81. data/lib/rouge/guesser.rb +2 -0
  82. data/lib/rouge/guessers/disambiguation.rb +35 -0
  83. data/lib/rouge/guessers/filename.rb +2 -0
  84. data/lib/rouge/guessers/glob_mapping.rb +3 -1
  85. data/lib/rouge/guessers/mimetype.rb +2 -0
  86. data/lib/rouge/guessers/modeline.rb +3 -1
  87. data/lib/rouge/guessers/source.rb +3 -1
  88. data/lib/rouge/guessers/util.rb +2 -0
  89. data/lib/rouge/lexer.rb +71 -15
  90. data/lib/rouge/lexers/abap.rb +13 -11
  91. data/lib/rouge/lexers/actionscript.rb +35 -34
  92. data/lib/rouge/lexers/ada.rb +162 -0
  93. data/lib/rouge/lexers/apache.rb +13 -15
  94. data/lib/rouge/lexers/apache/keywords.rb +15 -0
  95. data/lib/rouge/lexers/apex.rb +126 -0
  96. data/lib/rouge/lexers/apiblueprint.rb +2 -0
  97. data/lib/rouge/lexers/apple_script.rb +17 -14
  98. data/lib/rouge/lexers/armasm.rb +145 -0
  99. data/lib/rouge/lexers/awk.rb +26 -25
  100. data/lib/rouge/lexers/batchfile.rb +164 -0
  101. data/lib/rouge/lexers/bbcbasic.rb +112 -0
  102. data/lib/rouge/lexers/biml.rb +6 -4
  103. data/lib/rouge/lexers/bpf.rb +118 -0
  104. data/lib/rouge/lexers/brainfuck.rb +53 -0
  105. data/lib/rouge/lexers/bsl.rb +13 -12
  106. data/lib/rouge/lexers/c.rb +35 -57
  107. data/lib/rouge/lexers/ceylon.rb +7 -34
  108. data/lib/rouge/lexers/cfscript.rb +27 -26
  109. data/lib/rouge/lexers/clean.rb +156 -0
  110. data/lib/rouge/lexers/clojure.rb +15 -14
  111. data/lib/rouge/lexers/cmake.rb +16 -14
  112. data/lib/rouge/lexers/cmhg.rb +34 -0
  113. data/lib/rouge/lexers/coffeescript.rb +90 -49
  114. data/lib/rouge/lexers/common_lisp.rb +39 -38
  115. data/lib/rouge/lexers/conf.rb +7 -6
  116. data/lib/rouge/lexers/console.rb +92 -38
  117. data/lib/rouge/lexers/coq.rb +27 -23
  118. data/lib/rouge/lexers/cpp.rb +24 -13
  119. data/lib/rouge/lexers/crystal.rb +430 -0
  120. data/lib/rouge/lexers/csharp.rb +29 -29
  121. data/lib/rouge/lexers/css.rb +24 -23
  122. data/lib/rouge/lexers/csvs.rb +44 -0
  123. data/lib/rouge/lexers/cuda.rb +35 -0
  124. data/lib/rouge/lexers/cypher.rb +108 -0
  125. data/lib/rouge/lexers/cython.rb +151 -0
  126. data/lib/rouge/lexers/d.rb +64 -63
  127. data/lib/rouge/lexers/dart.rb +34 -33
  128. data/lib/rouge/lexers/datastudio.rb +138 -0
  129. data/lib/rouge/lexers/diff.rb +10 -4
  130. data/lib/rouge/lexers/digdag.rb +3 -1
  131. data/lib/rouge/lexers/docker.rb +12 -11
  132. data/lib/rouge/lexers/dot.rb +19 -18
  133. data/lib/rouge/lexers/ecl.rb +175 -0
  134. data/lib/rouge/lexers/eex.rb +52 -0
  135. data/lib/rouge/lexers/eiffel.rb +21 -20
  136. data/lib/rouge/lexers/elixir.rb +52 -36
  137. data/lib/rouge/lexers/elm.rb +30 -29
  138. data/lib/rouge/lexers/epp.rb +51 -0
  139. data/lib/rouge/lexers/erb.rb +5 -4
  140. data/lib/rouge/lexers/erlang.rb +1 -0
  141. data/lib/rouge/lexers/escape.rb +58 -0
  142. data/lib/rouge/lexers/factor.rb +41 -40
  143. data/lib/rouge/lexers/fortran.rb +36 -34
  144. data/lib/rouge/lexers/freefem.rb +240 -0
  145. data/lib/rouge/lexers/fsharp.rb +33 -31
  146. data/lib/rouge/lexers/gdscript.rb +171 -0
  147. data/lib/rouge/lexers/ghc_cmm.rb +340 -0
  148. data/lib/rouge/lexers/ghc_core.rb +151 -0
  149. data/lib/rouge/lexers/gherkin.rb +17 -14
  150. data/lib/rouge/lexers/gherkin/keywords.rb +2 -0
  151. data/lib/rouge/lexers/glsl.rb +2 -5
  152. data/lib/rouge/lexers/go.rb +3 -2
  153. data/lib/rouge/lexers/gradle.rb +1 -0
  154. data/lib/rouge/lexers/graphql.rb +78 -60
  155. data/lib/rouge/lexers/groovy.rb +24 -25
  156. data/lib/rouge/lexers/hack.rb +2 -1
  157. data/lib/rouge/lexers/haml.rb +26 -29
  158. data/lib/rouge/lexers/handlebars.rb +32 -20
  159. data/lib/rouge/lexers/haskell.rb +78 -61
  160. data/lib/rouge/lexers/haxe.rb +246 -0
  161. data/lib/rouge/lexers/hcl.rb +163 -0
  162. data/lib/rouge/lexers/hocon.rb +86 -0
  163. data/lib/rouge/lexers/hql.rb +139 -0
  164. data/lib/rouge/lexers/html.rb +36 -33
  165. data/lib/rouge/lexers/http.rb +10 -9
  166. data/lib/rouge/lexers/hylang.rb +15 -14
  167. data/lib/rouge/lexers/idlang.rb +34 -32
  168. data/lib/rouge/lexers/igorpro.rb +481 -225
  169. data/lib/rouge/lexers/ini.rb +13 -12
  170. data/lib/rouge/lexers/io.rb +10 -9
  171. data/lib/rouge/lexers/irb.rb +6 -5
  172. data/lib/rouge/lexers/isbl.rb +97 -0
  173. data/lib/rouge/lexers/isbl/builtins.rb +17 -0
  174. data/lib/rouge/lexers/java.rb +30 -26
  175. data/lib/rouge/lexers/javascript.rb +59 -61
  176. data/lib/rouge/lexers/jinja.rb +39 -22
  177. data/lib/rouge/lexers/jsl.rb +55 -0
  178. data/lib/rouge/lexers/json.rb +54 -11
  179. data/lib/rouge/lexers/json_doc.rb +8 -5
  180. data/lib/rouge/lexers/jsonnet.rb +19 -18
  181. data/lib/rouge/lexers/jsp.rb +120 -0
  182. data/lib/rouge/lexers/jsx.rb +18 -16
  183. data/lib/rouge/lexers/julia.rb +192 -74
  184. data/lib/rouge/lexers/kotlin.rb +79 -27
  185. data/lib/rouge/lexers/lasso.rb +54 -58
  186. data/lib/rouge/lexers/lasso/keywords.rb +14 -0
  187. data/lib/rouge/lexers/liquid.rb +116 -118
  188. data/lib/rouge/lexers/literate_coffeescript.rb +3 -2
  189. data/lib/rouge/lexers/literate_haskell.rb +6 -5
  190. data/lib/rouge/lexers/llvm.rb +66 -49
  191. data/lib/rouge/lexers/lua.rb +43 -4
  192. data/lib/rouge/lexers/lua/builtins.rb +2 -0
  193. data/lib/rouge/lexers/lustre.rb +79 -0
  194. data/lib/rouge/lexers/lutin.rb +33 -0
  195. data/lib/rouge/lexers/m68k.rb +143 -0
  196. data/lib/rouge/lexers/magik.rb +127 -0
  197. data/lib/rouge/lexers/make.rb +57 -34
  198. data/lib/rouge/lexers/markdown.rb +67 -39
  199. data/lib/rouge/lexers/mason.rb +110 -0
  200. data/lib/rouge/lexers/mathematica.rb +96 -0
  201. data/lib/rouge/lexers/mathematica/builtins.rb +13 -0
  202. data/lib/rouge/lexers/matlab.rb +26 -17
  203. data/lib/rouge/lexers/matlab/builtins.rb +4 -4
  204. data/lib/rouge/lexers/minizinc.rb +87 -0
  205. data/lib/rouge/lexers/moonscript.rb +4 -3
  206. data/lib/rouge/lexers/mosel.rb +64 -63
  207. data/lib/rouge/lexers/msgtrans.rb +26 -0
  208. data/lib/rouge/lexers/mxml.rb +19 -18
  209. data/lib/rouge/lexers/nasm.rb +43 -169
  210. data/lib/rouge/lexers/nesasm.rb +78 -0
  211. data/lib/rouge/lexers/nginx.rb +15 -14
  212. data/lib/rouge/lexers/nim.rb +4 -2
  213. data/lib/rouge/lexers/nix.rb +48 -42
  214. data/lib/rouge/lexers/objective_c.rb +5 -178
  215. data/lib/rouge/lexers/objective_c/common.rb +184 -0
  216. data/lib/rouge/lexers/objective_cpp.rb +31 -0
  217. data/lib/rouge/lexers/ocaml.rb +29 -64
  218. data/lib/rouge/lexers/ocaml/common.rb +53 -0
  219. data/lib/rouge/lexers/openedge.rb +429 -0
  220. data/lib/rouge/lexers/opentype_feature_file.rb +113 -0
  221. data/lib/rouge/lexers/pascal.rb +6 -5
  222. data/lib/rouge/lexers/perl.rb +103 -68
  223. data/lib/rouge/lexers/php.rb +91 -53
  224. data/lib/rouge/lexers/php/builtins.rb +183 -174
  225. data/lib/rouge/lexers/plain_text.rb +2 -1
  226. data/lib/rouge/lexers/plist.rb +16 -14
  227. data/lib/rouge/lexers/pony.rb +21 -20
  228. data/lib/rouge/lexers/powershell.rb +190 -638
  229. data/lib/rouge/lexers/praat.rb +76 -75
  230. data/lib/rouge/lexers/prolog.rb +27 -19
  231. data/lib/rouge/lexers/prometheus.rb +32 -30
  232. data/lib/rouge/lexers/properties.rb +13 -12
  233. data/lib/rouge/lexers/protobuf.rb +23 -22
  234. data/lib/rouge/lexers/puppet.rb +33 -32
  235. data/lib/rouge/lexers/python.rb +124 -99
  236. data/lib/rouge/lexers/q.rb +17 -14
  237. data/lib/rouge/lexers/qml.rb +13 -12
  238. data/lib/rouge/lexers/r.rb +13 -13
  239. data/lib/rouge/lexers/racket.rb +47 -22
  240. data/lib/rouge/lexers/reasonml.rb +65 -0
  241. data/lib/rouge/lexers/rego.rb +45 -0
  242. data/lib/rouge/lexers/robot_framework.rb +249 -0
  243. data/lib/rouge/lexers/ruby.rb +88 -71
  244. data/lib/rouge/lexers/rust.rb +45 -44
  245. data/lib/rouge/lexers/sas.rb +563 -0
  246. data/lib/rouge/lexers/sass.rb +9 -8
  247. data/lib/rouge/lexers/sass/common.rb +41 -40
  248. data/lib/rouge/lexers/scala.rb +67 -40
  249. data/lib/rouge/lexers/scheme.rb +19 -18
  250. data/lib/rouge/lexers/scss.rb +4 -3
  251. data/lib/rouge/lexers/sed.rb +31 -30
  252. data/lib/rouge/lexers/shell.rb +67 -55
  253. data/lib/rouge/lexers/sieve.rb +10 -9
  254. data/lib/rouge/lexers/slice.rb +32 -0
  255. data/lib/rouge/lexers/slim.rb +27 -26
  256. data/lib/rouge/lexers/smalltalk.rb +34 -33
  257. data/lib/rouge/lexers/smarty.rb +20 -19
  258. data/lib/rouge/lexers/sml.rb +68 -67
  259. data/lib/rouge/lexers/solidity.rb +185 -0
  260. data/lib/rouge/lexers/sparql.rb +129 -0
  261. data/lib/rouge/lexers/sqf.rb +109 -0
  262. data/lib/rouge/lexers/sqf/commands.rb +15 -0
  263. data/lib/rouge/lexers/sql.rb +47 -26
  264. data/lib/rouge/lexers/supercollider.rb +116 -0
  265. data/lib/rouge/lexers/swift.rb +50 -45
  266. data/lib/rouge/lexers/tap.rb +22 -20
  267. data/lib/rouge/lexers/tcl.rb +28 -27
  268. data/lib/rouge/lexers/terraform.rb +128 -0
  269. data/lib/rouge/lexers/tex.rb +20 -19
  270. data/lib/rouge/lexers/toml.rb +59 -20
  271. data/lib/rouge/lexers/tsx.rb +1 -0
  272. data/lib/rouge/lexers/ttcn3.rb +119 -0
  273. data/lib/rouge/lexers/tulip.rb +38 -36
  274. data/lib/rouge/lexers/turtle.rb +36 -38
  275. data/lib/rouge/lexers/twig.rb +1 -0
  276. data/lib/rouge/lexers/typescript.rb +12 -0
  277. data/lib/rouge/lexers/typescript/common.rb +1 -0
  278. data/lib/rouge/lexers/vala.rb +19 -18
  279. data/lib/rouge/lexers/varnish.rb +168 -0
  280. data/lib/rouge/lexers/vb.rb +28 -27
  281. data/lib/rouge/lexers/verilog.rb +27 -28
  282. data/lib/rouge/lexers/vhdl.rb +13 -12
  283. data/lib/rouge/lexers/viml.rb +15 -14
  284. data/lib/rouge/lexers/viml/keywords.rb +2 -0
  285. data/lib/rouge/lexers/vue.rb +19 -12
  286. data/lib/rouge/lexers/wollok.rb +27 -26
  287. data/lib/rouge/lexers/xml.rb +18 -21
  288. data/lib/rouge/lexers/xojo.rb +61 -0
  289. data/lib/rouge/lexers/xpath.rb +332 -0
  290. data/lib/rouge/lexers/xquery.rb +145 -0
  291. data/lib/rouge/lexers/yaml.rb +64 -61
  292. data/lib/rouge/lexers/yang.rb +147 -0
  293. data/lib/rouge/plugins/redcarpet.rb +9 -2
  294. data/lib/rouge/regex_lexer.rb +27 -25
  295. data/lib/rouge/template_lexer.rb +1 -0
  296. data/lib/rouge/tex_theme_renderer.rb +132 -0
  297. data/lib/rouge/text_analyzer.rb +1 -0
  298. data/lib/rouge/theme.rb +5 -0
  299. data/lib/rouge/themes/base16.rb +1 -0
  300. data/lib/rouge/themes/bw.rb +41 -0
  301. data/lib/rouge/themes/colorful.rb +1 -0
  302. data/lib/rouge/themes/github.rb +1 -0
  303. data/lib/rouge/themes/gruvbox.rb +1 -0
  304. data/lib/rouge/themes/igor_pro.rb +1 -0
  305. data/lib/rouge/themes/magritte.rb +78 -0
  306. data/lib/rouge/themes/molokai.rb +1 -0
  307. data/lib/rouge/themes/monokai.rb +1 -0
  308. data/lib/rouge/themes/monokai_sublime.rb +4 -1
  309. data/lib/rouge/themes/pastie.rb +2 -1
  310. data/lib/rouge/themes/thankful_eyes.rb +2 -1
  311. data/lib/rouge/themes/tulip.rb +2 -1
  312. data/lib/rouge/token.rb +31 -22
  313. data/lib/rouge/util.rb +3 -2
  314. data/lib/rouge/version.rb +2 -1
  315. data/rouge.gemspec +6 -3
  316. metadata +136 -8
  317. data/lib/rouge/lexers/apache/keywords.yml +0 -764
  318. data/lib/rouge/lexers/lasso/keywords.yml +0 -446
@@ -0,0 +1,37 @@
1
+ # -*- coding: utf-8 -*- #
2
+ # frozen_string_literal: true
3
+ module Rouge
4
+ module Formatters
5
+ class TerminalTruecolor < Terminal256
6
+ tag 'terminal_truecolor'
7
+
8
+ class TruecolorEscapeSequence < Terminal256::EscapeSequence
9
+ def style_string
10
+ @style_string ||= begin
11
+ out = String.new('')
12
+ out << escape(['48', '2', *get_rgb(style.bg)]) if style.bg
13
+ out << escape(['38', '2', *get_rgb(style.fg)]) if style.fg
14
+ out << escape(['1']) if style[:bold] || style[:italic]
15
+ out
16
+ end
17
+ end
18
+
19
+ def get_rgb(color)
20
+ color = $1 if color =~ /#(\h+)/
21
+
22
+ case color.size
23
+ when 3 then color.chars.map { |c| c.to_i(16) * 2 }
24
+ when 6 then color.scan(/../).map { |cc| cc.to_i(16) }
25
+ else
26
+ raise "invalid color: #{color.inspect}"
27
+ end
28
+ end
29
+ end
30
+
31
+ # @override
32
+ def make_escape_sequence(style)
33
+ TruecolorEscapeSequence.new(style)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,92 @@
1
+ # -*- coding: utf-8 -*- #
2
+ # frozen_string_literal: true
3
+
4
+ module Rouge
5
+ module Formatters
6
+ class Tex < Formatter
7
+ tag 'tex'
8
+
9
+ # A map of TeX escape characters.
10
+ # Newlines are handled specially by using #token_lines
11
+ # spaces are preserved as long as they aren't at the beginning
12
+ # of a line. see #tag_first for our initial-space strategy
13
+ ESCAPE = {
14
+ '&' => '\&',
15
+ '%' => '\%',
16
+ '$' => '\$',
17
+ '#' => '\#',
18
+ '_' => '\_',
19
+ '{' => '\{',
20
+ '}' => '\}',
21
+ '~' => '{\textasciitilde}',
22
+ '^' => '{\textasciicircum}',
23
+ '|' => '{\textbar}',
24
+ '\\' => '{\textbackslash}',
25
+ '`' => '{\textasciigrave}',
26
+ "'" => "'{}",
27
+ '"' => '"{}',
28
+ "\t" => '{\tab}',
29
+ }
30
+
31
+ ESCAPE_REGEX = /[#{ESCAPE.keys.map(&Regexp.method(:escape)).join}]/om
32
+
33
+ def initialize(opts={})
34
+ @prefix = opts.fetch(:prefix) { 'RG' }
35
+ end
36
+
37
+ def escape_tex(str)
38
+ str.gsub(ESCAPE_REGEX, ESCAPE)
39
+ end
40
+
41
+ def stream(tokens, &b)
42
+ # surround the output with \begin{RG*}...\end{RG*}
43
+ yield "\\begin{#{@prefix}*}%\n"
44
+
45
+ # we strip the newline off the last line to avoid
46
+ # an extra line being rendered. we do this by yielding
47
+ # the \newline tag *before* every line group except
48
+ # the first.
49
+ first = true
50
+
51
+ token_lines tokens do |line|
52
+ if first
53
+ first = false
54
+ else
55
+ yield "\\newline%\n"
56
+ end
57
+
58
+ render_line(line, &b)
59
+ end
60
+
61
+ yield "%\n\\end{#{@prefix}*}%\n"
62
+ end
63
+
64
+ def render_line(line, &b)
65
+ line.each do |(tok, val)|
66
+ hphantom_tag(tok, val, &b)
67
+ end
68
+ end
69
+
70
+ # Special handling for leading spaces, since they may be gobbled
71
+ # by a previous command. We replace all initial spaces with
72
+ # \hphantom{xxxx}, which renders an empty space equal to the size
73
+ # of the x's.
74
+ def hphantom_tag(tok, val)
75
+ leading = nil
76
+ val.sub!(/^[ ]+/) { leading = $&.size; '' }
77
+ yield "\\hphantom{#{'x' * leading}}" if leading
78
+ yield tag(tok, val) unless val.empty?
79
+ end
80
+
81
+ def tag(tok, val)
82
+ if escape?(tok)
83
+ val
84
+ elsif tok == Token::Tokens::Text
85
+ escape_tex(val)
86
+ else
87
+ "\\#@prefix{#{tok.shortname}}{#{escape_tex(val)}}"
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rouge
2
4
  class Guesser
3
5
  class Ambiguous < StandardError
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rouge
2
4
  module Guessers
3
5
  class Disambiguation < Guesser
@@ -73,6 +75,8 @@ module Rouge
73
75
  disambiguate '*.h' do
74
76
  next ObjectiveC if matches?(/@(end|implementation|protocol|property)\b/)
75
77
  next ObjectiveC if contains?('@"')
78
+ next Cpp if matches?(/^\s*(?:catch|class|constexpr|namespace|private|
79
+ protected|public|template|throw|try|using)\b/x)
76
80
 
77
81
  C
78
82
  end
@@ -81,7 +85,14 @@ module Rouge
81
85
  next ObjectiveC if matches?(/@(end|implementation|protocol|property)\b/)
82
86
  next ObjectiveC if contains?('@"')
83
87
 
88
+ next Mathematica if contains?('(*')
89
+ next Mathematica if contains?(':=')
90
+
91
+ next Mason if matches?(/<%(def|method|text|doc|args|flags|attr|init|once|shared|perl|cleanup|filter)([^>]*)(>)/)
92
+
84
93
  next Matlab if matches?(/^\s*?%/)
94
+
95
+ next Mason if matches? %r!(</?%|<&)!
85
96
  end
86
97
 
87
98
  disambiguate '*.php' do
@@ -96,6 +107,30 @@ module Rouge
96
107
 
97
108
  Cpp
98
109
  end
110
+
111
+ disambiguate '*.plist' do
112
+ next XML if matches?(/\A<\?xml\b/)
113
+
114
+ Plist
115
+ end
116
+
117
+ disambiguate '*.sc' do
118
+ next Python if matches?(/^#/)
119
+ next SuperCollider if matches?(/(?:^~|;$)/)
120
+
121
+ next Python
122
+ end
123
+
124
+ disambiguate 'Messages' do
125
+ next MsgTrans if matches?(/^[^\s:]+:[^\s:]+/)
126
+
127
+ next PlainText
128
+ end
129
+
130
+ disambiguate '*.cls' do
131
+ next TeX if matches?(/\A\s*(?:\\|%)/)
132
+ next Apex
133
+ end
99
134
  end
100
135
  end
101
136
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rouge
2
4
  module Guessers
3
5
  class Filename < Guesser
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rouge
2
4
  module Guessers
3
5
  # This class allows for custom behavior
@@ -30,7 +32,7 @@ module Rouge
30
32
  basename = File.basename(filename)
31
33
 
32
34
  collect_best(lexers) do |lexer|
33
- score = (@glob_map[lexer.name] || []).map do |pattern|
35
+ (@glob_map[lexer.name] || []).map do |pattern|
34
36
  if test_glob(pattern, basename)
35
37
  # specificity is better the fewer wildcards there are
36
38
  -pattern.scan(/[*?\[]/).size
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rouge
2
4
  module Guessers
3
5
  class Mimetype < Guesser
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rouge
2
4
  module Guessers
3
5
  class Modeline < Guesser
@@ -35,7 +37,7 @@ module Rouge
35
37
 
36
38
  matches = MODELINES.map { |re| re.match(search_space) }.compact
37
39
  return lexers unless matches.any?
38
-
40
+
39
41
  match_set = Set.new(matches.map { |m| m[1] })
40
42
  lexers.select { |l| match_set.include?(l.tag) || l.aliases.any? { |a| match_set.include?(a) } }
41
43
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rouge
2
4
  module Guessers
3
5
  class Source < Guesser
@@ -20,7 +22,7 @@ module Rouge
20
22
  source_text = TextAnalyzer.new(source_text)
21
23
 
22
24
  collect_best(lexers) do |lexer|
23
- next unless lexer.methods(false).include? :detect?
25
+ next unless lexer.detectable?
24
26
  lexer.detect?(source_text) ? 1 : nil
25
27
  end
26
28
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rouge
2
4
  module Guessers
3
5
  module Util
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*- #
2
+ # frozen_string_literal: true
2
3
 
3
4
  # stdlib
4
5
  require 'strscan'
@@ -22,6 +23,14 @@ module Rouge
22
23
  new(opts).lex(stream, &b)
23
24
  end
24
25
 
26
+ # In case #continue_lex is called statically, we simply
27
+ # begin a new lex from the beginning, since there is no state.
28
+ #
29
+ # @see #continue_lex
30
+ def continue_lex(*a, &b)
31
+ lex(*a, &b)
32
+ end
33
+
25
34
  # Given a name in string, return the correct lexer class.
26
35
  # @param [String] name
27
36
  # @return [Class<Rouge::Lexer>,nil]
@@ -40,6 +49,9 @@ module Rouge
40
49
  #
41
50
  # Lexer.find_fancy('guess', "#!/bin/bash\necho Hello, world")
42
51
  #
52
+ # If the code matches more than one lexer then Guesser::Ambiguous
53
+ # is raised.
54
+ #
43
55
  # This is used in the Redcarpet plugin as well as Rouge's own
44
56
  # markdown lexer for highlighting internal code blocks.
45
57
  #
@@ -105,7 +117,7 @@ module Rouge
105
117
  def demo_file(arg=:absent)
106
118
  return @demo_file = Pathname.new(arg) unless arg == :absent
107
119
 
108
- @demo_file = Pathname.new(__FILE__).dirname.join('demos', tag)
120
+ @demo_file = Pathname.new(File.join(__dir__, 'demos', tag))
109
121
  end
110
122
 
111
123
  # Specify or get a small demo string for this lexer
@@ -117,7 +129,7 @@ module Rouge
117
129
 
118
130
  # @return a list of all lexers.
119
131
  def all
120
- registry.values.uniq
132
+ @all ||= registry.values.uniq
121
133
  end
122
134
 
123
135
  # Guess which lexer to use based on a hash of info.
@@ -187,16 +199,25 @@ module Rouge
187
199
  end
188
200
 
189
201
  def disable_debug!
190
- @debug_enabled = false
202
+ remove_instance_variable :@debug_enabled if defined? @debug_enabled
191
203
  end
192
204
 
193
205
  def debug_enabled?
194
- !!@debug_enabled
206
+ (defined? @debug_enabled) ? true : false
207
+ end
208
+
209
+ # Determine if a lexer has a method named +:detect?+ defined in its
210
+ # singleton class.
211
+ def detectable?
212
+ return @detectable if defined?(@detectable)
213
+ @detectable = singleton_methods(false).include?(:detect?)
195
214
  end
196
215
 
197
216
  protected
198
217
  # @private
199
218
  def register(name, lexer)
219
+ # reset an existing list of lexers
220
+ @all = nil if defined?(@all)
200
221
  registry[name.to_s] = lexer
201
222
  end
202
223
 
@@ -235,6 +256,13 @@ module Rouge
235
256
 
236
257
  # Specify a list of filename globs associated with this lexer.
237
258
  #
259
+ # If a filename glob is associated with more than one lexer, this can
260
+ # cause a Guesser::Ambiguous error to be raised in various guessing
261
+ # methods. These errors can be avoided by disambiguation. Filename globs
262
+ # are disambiguated in one of two ways. Either the lexer will define a
263
+ # `self.detect?` method (intended for use with shebangs and doctypes) or a
264
+ # manual rule will be specified in Guessers::Disambiguation.
265
+ #
238
266
  # @example
239
267
  # class Ruby < Lexer
240
268
  # filenames '*.rb', '*.ruby', 'Gemfile', 'Rakefile'
@@ -255,7 +283,9 @@ module Rouge
255
283
 
256
284
  # @private
257
285
  def assert_utf8!(str)
258
- return if %w(US-ASCII UTF-8 ASCII-8BIT).include? str.encoding.name
286
+ encoding = str.encoding.name
287
+ return if encoding == 'US-ASCII' || encoding == 'UTF-8' || encoding == 'ASCII-8BIT'
288
+
259
289
  raise EncodingError.new(
260
290
  "Bad encoding: #{str.encoding.names.join(',')}. " +
261
291
  "Please convert your string to UTF-8."
@@ -284,12 +314,12 @@ module Rouge
284
314
  @options = {}
285
315
  opts.each { |k, v| @options[k.to_s] = v }
286
316
 
287
- @debug = Lexer.debug_enabled? && bool_option(:debug)
317
+ @debug = Lexer.debug_enabled? && bool_option('debug')
288
318
  end
289
319
 
290
320
  def as_bool(val)
291
321
  case val
292
- when nil, false, 0, '0', 'off'
322
+ when nil, false, 0, '0', 'false', 'off'
293
323
  false
294
324
  when Array
295
325
  val.empty? ? true : as_bool(val.last)
@@ -339,8 +369,10 @@ module Rouge
339
369
  end
340
370
 
341
371
  def bool_option(name, &default)
342
- if @options.key?(name.to_s)
343
- as_bool(@options[name.to_s])
372
+ name_str = name.to_s
373
+
374
+ if @options.key?(name_str)
375
+ as_bool(@options[name_str])
344
376
  else
345
377
  default ? default.call : false
346
378
  end
@@ -392,12 +424,37 @@ module Rouge
392
424
  #
393
425
  # @option opts :continue
394
426
  # Continue the lex from the previous state (i.e. don't call #reset!)
395
- def lex(string, opts={}, &b)
396
- return enum_for(:lex, string, opts) unless block_given?
427
+ #
428
+ # @note The use of :continue => true has been deprecated. A warning is
429
+ # issued if run with `$VERBOSE` set to true.
430
+ #
431
+ # @note The use of arbitrary `opts` has never been supported, but we
432
+ # previously ignored them with no error. We now warn unconditionally.
433
+ def lex(string, opts=nil, &b)
434
+ if opts
435
+ if (opts.keys - [:continue]).size > 0
436
+ # improper use of options hash
437
+ warn('Improper use of Lexer#lex - this method does not receive options.' +
438
+ ' This will become an error in a future version.')
439
+ end
440
+
441
+ if opts[:continue]
442
+ warn '`lex :continue => true` is deprecated, please use #continue_lex instead'
443
+ return continue_lex(string, &b)
444
+ end
445
+ end
446
+
447
+ return enum_for(:lex, string) unless block_given?
397
448
 
398
449
  Lexer.assert_utf8!(string)
450
+ reset!
451
+
452
+ continue_lex(string, &b)
453
+ end
399
454
 
400
- reset! unless opts[:continue]
455
+ # Continue the lex from the the current state without resetting
456
+ def continue_lex(string, &b)
457
+ return enum_for(:continue_lex, string, &b) unless block_given?
401
458
 
402
459
  # consolidate consecutive tokens of the same type
403
460
  last_token = nil
@@ -448,14 +505,13 @@ module Rouge
448
505
  end
449
506
 
450
507
  module Lexers
508
+ BASE_DIR = "#{__dir__}/lexers".freeze
451
509
  @_loaded_lexers = {}
452
510
 
453
511
  def self.load_lexer(relpath)
454
512
  return if @_loaded_lexers.key?(relpath)
455
513
  @_loaded_lexers[relpath] = true
456
-
457
- root = Pathname.new(__FILE__).dirname.join('lexers')
458
- load root.join(relpath)
514
+ load File.join(BASE_DIR, relpath)
459
515
  end
460
516
  end
461
517
  end
@@ -1,4 +1,6 @@
1
1
  # -*- coding: utf-8 -*- #
2
+ # frozen_string_literal: true
3
+
2
4
  # ABAP elements taken from http://help.sap.com/abapdocu_750/en/index.htm?file=abapdo.htm
3
5
 
4
6
  module Rouge
@@ -180,16 +182,16 @@ module Rouge
180
182
  end
181
183
 
182
184
  state :root do
183
- rule /\s+/m, Text
185
+ rule %r/\s+/m, Text
184
186
 
185
- rule /".*/, Comment::Single
187
+ rule %r/".*/, Comment::Single
186
188
  rule %r(^\*.*), Comment::Multiline
187
- rule /\d+/, Num::Integer
188
- rule /('|`)/, Str::Single, :single_string
189
- rule /[\[\]\(\)\{\}\[\]\.,:\|]/, Punctuation
189
+ rule %r/\d+/, Num::Integer
190
+ rule %r/('|`)/, Str::Single, :single_string
191
+ rule %r/[\[\]\(\)\{\}\.,:\|]/, Punctuation
190
192
 
191
193
  # builtins / new ABAP 7.40 keywords (@DATA(), ...)
192
- rule /(->|=>)?([A-Za-z][A-Za-z0-9_\-]*)(\()/ do |m|
194
+ rule %r/(->|=>)?([A-Za-z][A-Za-z0-9_\-]*)(\()/ do |m|
193
195
  if m[1] != ''
194
196
  token Operator, m[1]
195
197
  end
@@ -205,7 +207,7 @@ module Rouge
205
207
  end
206
208
 
207
209
  # keywords, types and normal text
208
- rule /\w[\w\d]*/ do |m|
210
+ rule %r/\w\w*/ do |m|
209
211
  if self.class.keywords.include? m[0].upcase
210
212
  token Keyword
211
213
  elsif self.class.types.include? m[0].downcase
@@ -227,10 +229,10 @@ module Rouge
227
229
  end
228
230
 
229
231
  state :single_string do
230
- rule /\\./, Str::Escape
231
- rule /(''|``)/, Str::Escape
232
- rule /['`]/, Str::Single, :pop!
233
- rule /[^\\'`]+/, Str::Single
232
+ rule %r/\\./, Str::Escape
233
+ rule %r/(''|``)/, Str::Escape
234
+ rule %r/['`]/, Str::Single, :pop!
235
+ rule %r/[^\\'`]+/, Str::Single
234
236
  end
235
237
 
236
238
  end