Skip to content

Commit

Permalink
Merge branch 'moulin' into moulin.feature.openedge-abl-lexer
Browse files Browse the repository at this point in the history
The branch that was the source of the original pull request (jneen#1033)
has been deleted. In order to run the CI tests, the pull request has
been checked out locally and this merges it with the other changes made
in the moulin branch.
  • Loading branch information
pyrmont committed May 12, 2019
2 parents a80fc7f + 2046573 commit 84df404
Show file tree
Hide file tree
Showing 71 changed files with 2,785 additions and 286 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Expand Up @@ -2,12 +2,10 @@ sudo: false
dist: trusty
language: ruby
rvm:
- "2.0"
- "2.1"
- "2.2"
- "2.3"
- "2.4"
- "2.5"
- "2.6"

cache:
bundler: true
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -4,7 +4,7 @@ source 'http://rubygems.org'

gemspec

gem 'bundler', '~> 1.15'
gem 'bundler', '~> 2.0'
gem 'rake'

gem 'minitest', '>= 5.0'
Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -47,6 +47,10 @@ Builtin formatters include:
This formatter will split your code into lines, each contained in its own div. The
`class` option will be used to add a class name to the div, given the line
number.
* `Rouge::Formatters::HTMLLineanchors.new(formatter, class_format: 'line-%i')` This formatter
will generate an anchor link before each line. The `class_format` option will be used to add
a class name to the anchor, given the line number. This works the same as the `lineanchors`
option in Pygments.
* `Rouge::Formatters::HTMLPygments.new(formatter, css_class='codehilite')`
wraps the given formatter with div wrappers generally expected by stylesheets designed for
Pygments.
Expand Down
87 changes: 46 additions & 41 deletions lib/rouge.rb
Expand Up @@ -34,51 +34,56 @@ def highlight(text, lexer, formatter, &b)
end
end

load_dir = Pathname.new(__FILE__).dirname
load load_dir.join('rouge/version.rb')
# mimic Kernel#require_relative API
def load_relative(path)
load File.join(__dir__, "#{path}.rb")
end

load load_dir.join('rouge/util.rb')
def lexer_dir(path = '')
File.join(__dir__, 'rouge', 'lexers', path)
end

load load_dir.join('rouge/text_analyzer.rb')
load load_dir.join('rouge/token.rb')
load_relative 'rouge/version'
load_relative 'rouge/util'
load_relative 'rouge/text_analyzer'
load_relative 'rouge/token'

load load_dir.join('rouge/lexer.rb')
load load_dir.join('rouge/regex_lexer.rb')
load load_dir.join('rouge/template_lexer.rb')
load_relative 'rouge/lexer'
load_relative 'rouge/regex_lexer'
load_relative 'rouge/template_lexer'

lexers_dir = load_dir.join('rouge/lexers')
Dir.glob(lexers_dir.join('*.rb')).each do |f|
Rouge::Lexers.load_lexer(Pathname.new(f).relative_path_from(lexers_dir).to_s)
end
Dir.glob(lexer_dir('*rb')).each { |f| Rouge::Lexers.load_lexer(f.sub(lexer_dir, '')) }

load load_dir.join('rouge/guesser.rb')
load load_dir.join('rouge/guessers/util.rb')
load load_dir.join('rouge/guessers/glob_mapping.rb')
load load_dir.join('rouge/guessers/modeline.rb')
load load_dir.join('rouge/guessers/filename.rb')
load load_dir.join('rouge/guessers/mimetype.rb')
load load_dir.join('rouge/guessers/source.rb')
load load_dir.join('rouge/guessers/disambiguation.rb')
load_relative 'rouge/guesser'
load_relative 'rouge/guessers/util'
load_relative 'rouge/guessers/glob_mapping'
load_relative 'rouge/guessers/modeline'
load_relative 'rouge/guessers/filename'
load_relative 'rouge/guessers/mimetype'
load_relative 'rouge/guessers/source'
load_relative 'rouge/guessers/disambiguation'

load load_dir.join('rouge/formatter.rb')
load load_dir.join('rouge/formatters/html.rb')
load load_dir.join('rouge/formatters/html_table.rb')
load load_dir.join('rouge/formatters/html_pygments.rb')
load load_dir.join('rouge/formatters/html_legacy.rb')
load load_dir.join('rouge/formatters/html_linewise.rb')
load load_dir.join('rouge/formatters/html_inline.rb')
load load_dir.join('rouge/formatters/terminal256.rb')
load load_dir.join('rouge/formatters/null.rb')
load_relative 'rouge/formatter'
load_relative 'rouge/formatters/html'
load_relative 'rouge/formatters/html_table'
load_relative 'rouge/formatters/html_pygments'
load_relative 'rouge/formatters/html_legacy'
load_relative 'rouge/formatters/html_lineanchors'
load_relative 'rouge/formatters/html_linewise'
load_relative 'rouge/formatters/html_inline'
load_relative 'rouge/formatters/terminal256'
load_relative 'rouge/formatters/null'

load load_dir.join('rouge/theme.rb')
load load_dir.join('rouge/themes/thankful_eyes.rb')
load load_dir.join('rouge/themes/colorful.rb')
load load_dir.join('rouge/themes/base16.rb')
load load_dir.join('rouge/themes/github.rb')
load load_dir.join('rouge/themes/igor_pro.rb')
load load_dir.join('rouge/themes/monokai.rb')
load load_dir.join('rouge/themes/molokai.rb')
load load_dir.join('rouge/themes/monokai_sublime.rb')
load load_dir.join('rouge/themes/gruvbox.rb')
load load_dir.join('rouge/themes/tulip.rb')
load load_dir.join('rouge/themes/pastie.rb')
load_relative 'rouge/theme'
load_relative 'rouge/themes/thankful_eyes'
load_relative 'rouge/themes/colorful'
load_relative 'rouge/themes/base16'
load_relative 'rouge/themes/github'
load_relative 'rouge/themes/igor_pro'
load_relative 'rouge/themes/monokai'
load_relative 'rouge/themes/molokai'
load_relative 'rouge/themes/monokai_sublime'
load_relative 'rouge/themes/gruvbox'
load_relative 'rouge/themes/tulip'
load_relative 'rouge/themes/pastie'
load_relative 'rouge/themes/ayu_light'
8 changes: 8 additions & 0 deletions lib/rouge/demos/csvs
@@ -0,0 +1,8 @@
version 1.1
@totalColumns 5
@separator ','
Transaction_Date: xDate
Transaction_ID: notEmpty
Originator_Name: notEmpty
Originator_Address: any("yes","no")
Originator_Country: notEmpty
1 change: 1 addition & 0 deletions lib/rouge/demos/m68k
@@ -1,3 +1,4 @@
move
initialize: ; go into super user mode
clr.l -(a7)
move.w #32,-(a7)
Expand Down
58 changes: 58 additions & 0 deletions lib/rouge/demos/magik
@@ -0,0 +1,58 @@
def_slotted_exemplar(
:test_object,
{
{:slot, _unset}
})
$

_method test_object.new()
_return _clone.init()
_endmethod
$

_method test_object.init()
## initializer
_return _self
_endmethod
$

_method test_object.run(arg_1, _gather args)
## Does something.
write("calling test_object.run()")
write(%tab, %newline)
show(:args, _scatter args)

_local x <<
_if arg_1 _is :start
_then
>> 10.0
_endif
_local results << rope.new()

_for arg _over rgs.fast_elements()
_loop
results.add_last(arg)
_endloop

_local stream << external_text_output_stream.new("$HOME/test.txt")
_protect
local y << p_args.map(_proc(p_obj)
>> p_obj + 1
_endproc
_protection
stream.close()
_endprotect

_return results
_endmethod
$

_block
_local obj << test_object.new()
_try _with cond
_local results << obj.run()
_when error
write("Caught error: ", cond.report_string)
_endtry
endblock
$
13 changes: 13 additions & 0 deletions lib/rouge/demos/sas
@@ -0,0 +1,13 @@
data sim;
do i = 1 to 100;
x1 = rand("Normal");
x2 = rand("Binomial", 0.5, 100);
output;
end;
run;

proc means data=sashelp.class;
class sex;
var height weight;
output out = mean_by_sex;
run;
10 changes: 10 additions & 0 deletions lib/rouge/demos/xojo
@@ -0,0 +1,10 @@
Dim f As FolderItem
f = GetOpenFolderItem(FileTypes1.jpeg) // defined in the File Type Set editor
If not f.Exists Then
Beep
MsgBox("The file " + f.NativePath + "doesn't exist.")
Else // document exists
ImageWell1.image=Picture.Open(f)
End If
Exception err As NilObjectException
MsgBox("Invalid pathname!")
5 changes: 5 additions & 0 deletions lib/rouge/formatters/html_legacy.rb
Expand Up @@ -11,7 +11,9 @@ class HTMLLegacy < Formatter
tag 'html_legacy'

# @option opts [String] :css_class ('highlight')
# @option opts [true/false] :line_anchors (false)
# @option opts [true/false] :line_numbers (false)
# @option opts [true/false] :linewise (false)
# @option opts [Rouge::CSSTheme] :inline_theme (nil)
# @option opts [true/false] :wrap (true)
#
Expand All @@ -28,6 +30,9 @@ def initialize(opts={})
@formatter = opts[:inline_theme] ? HTMLInline.new(opts[:inline_theme])
: HTML.new

@formatter = HTMLLineanchors.new(@formatter, opts) if opts[:lineanchors]

@formatter = HTMLLinewise.new(@formatter, opts) if opts[:linewise]

@formatter = HTMLTable.new(@formatter, opts) if opts[:line_numbers]

Expand Down
27 changes: 27 additions & 0 deletions lib/rouge/formatters/html_lineanchors.rb
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*- #

module Rouge
module Formatters
class HTMLLineanchors < Formatter
def initialize(formatter, opts={})
@formatter = formatter
@class_format = opts.fetch(:class, 'line-%i')
end

def stream(tokens, &b)
token_lines(tokens) do |line|
yield "<a name=#{next_line_class}></a>"
line.each do |tok, val|
yield @formatter.span(tok, val)
end
yield "\n"
end
end

def next_line_class
@lineno ||= 0
sprintf(@class_format, @lineno += 1).inspect
end
end
end
end
7 changes: 5 additions & 2 deletions lib/rouge/formatters/html_linewise.rb
Expand Up @@ -6,16 +6,19 @@ module Formatters
class HTMLLinewise < Formatter
def initialize(formatter, opts={})
@formatter = formatter
@tag_name = opts.fetch(:tag_name, 'div')
@class_format = opts.fetch(:class, 'line-%i')
end

def stream(tokens, &b)
first_line = true
token_lines(tokens) do |line|
yield "<div class=#{next_line_class}>"
first_line ? first_line = false : yield("\n")
yield "<#{@tag_name} class=#{next_line_class}>"
line.each do |tok, val|
yield @formatter.span(tok, val)
end
yield '</div>'
yield "</#{@tag_name}>"
end
end

Expand Down
6 changes: 6 additions & 0 deletions lib/rouge/guessers/disambiguation.rb
Expand Up @@ -101,6 +101,12 @@ def match?(filename)

Cpp
end

disambiguate '*.plist' do
next XML if matches?(/\A<\?xml\b/)

Plist
end
end
end
end
4 changes: 1 addition & 3 deletions lib/rouge/lexer.rb
Expand Up @@ -454,9 +454,7 @@ module Lexers
def self.load_lexer(relpath)
return if @_loaded_lexers.key?(relpath)
@_loaded_lexers[relpath] = true

root = Pathname.new(__FILE__).dirname.join('lexers')
load root.join(relpath)
load File.join(__dir__, 'lexers', relpath)
end
end
end
35 changes: 6 additions & 29 deletions lib/rouge/lexers/c.rb
Expand Up @@ -106,7 +106,7 @@ def self.builtins
rule /\d+[lu]*/i, Num::Integer
rule %r(\*/), Error
rule %r([~!%^&*+=\|?:<>/-]), Operator
rule /[()\[\],.]/, Punctuation
rule /[()\[\],.;]/, Punctuation
rule /\bcase\b/, Keyword, :case
rule /(?:true|false|NULL)\b/, Name::Builtin
rule id do |m|
Expand All @@ -133,47 +133,24 @@ def self.builtins

state :root do
mixin :expr_whitespace

# functions
rule %r(
([\w*\s]+?[\s*]) # return arguments
(#{id}) # function name
(\s*\([^;]*?\)) # signature
(#{ws})({) # open brace
(#{ws}?)({|;) # open brace or semicolon
)mx do |m|
# TODO: do this better.
recurse m[1]
token Name::Function, m[2]
recurse m[3]
recurse m[4]
token Punctuation, m[5]
push :function
end

# function declarations
rule %r(
([\w*\s]+?[\s*]) # return arguments
(#{id}) # function name
(\s*\([^;]*?\)) # signature
(#{ws})(;) # semicolon
)mx do |m|
# TODO: do this better.
recurse m[1]
token Name::Function, m[2]
recurse m[3]
recurse m[4]
token Punctuation, m[5]
push :statement
if m[5] == ?{
push :function
end
end

rule(//) { push :statement }
end

state :statement do
rule /;/, Punctuation, :pop!
mixin :expr_whitespace
rule /\{/, Punctuation, :function
mixin :statements
rule /[{}]/, Punctuation
end

state :function do
Expand Down

0 comments on commit 84df404

Please sign in to comment.