texas 0.1.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.
- data/bin/texas +6 -0
- data/contents/_chapter.md.erb +13 -0
- data/contents/_preambel.tex.erb +50 -0
- data/contents/master.tex.erb +7 -0
- data/lib/texas/build/base.rb +120 -0
- data/lib/texas/build/dry.rb +11 -0
- data/lib/texas/build/final.rb +7 -0
- data/lib/texas/build/task/add_default_templates_to_build_path.rb +22 -0
- data/lib/texas/build/task/base.rb +15 -0
- data/lib/texas/build/task/copy_contents_to_build_path.rb +31 -0
- data/lib/texas/build/task/open_pdf.rb +36 -0
- data/lib/texas/build/task/publish_pdf.rb +46 -0
- data/lib/texas/build/task/rewrite_marked_templates.rb +13 -0
- data/lib/texas/build/task/run_before_scripts.rb +22 -0
- data/lib/texas/build/task/run_master_template.rb +23 -0
- data/lib/texas/build.rb +15 -0
- data/lib/texas/core_ext/string.rb +132 -0
- data/lib/texas/core_ext.rb +1 -0
- data/lib/texas/option_parser.rb +175 -0
- data/lib/texas/runner.rb +71 -0
- data/lib/texas/task/base.rb +23 -0
- data/lib/texas/task/new.rb +18 -0
- data/lib/texas/task/watch.rb +39 -0
- data/lib/texas/task.rb +11 -0
- data/lib/texas/template/helper/base.rb +82 -0
- data/lib/texas/template/helper/info.rb +29 -0
- data/lib/texas/template/helper/md.rb +35 -0
- data/lib/texas/template/helper/tex.rb +83 -0
- data/lib/texas/template/helper.rb +6 -0
- data/lib/texas/template/runner/base.rb +70 -0
- data/lib/texas/template/runner/md.rb +16 -0
- data/lib/texas/template/runner/tex.rb +11 -0
- data/lib/texas/template/runner.rb +13 -0
- data/lib/texas/template.rb +62 -0
- data/lib/texas/version.rb +9 -0
- data/lib/texas.rb +18 -0
- data/spec/fixtures/basic/contents/contents.tex.erb +8 -0
- data/spec/fixtures/basic/figures/test_figure.png +0 -0
- data/spec/fixtures/basic-md/contents/contents.md.erb +1 -0
- data/spec/fixtures/basic-md/contents/contents.tex.should +3 -0
- data/spec/fixtures/basic-md/contents/input_template.md.erb +3 -0
- data/spec/fixtures/basic-tex/contents/contents.tex.erb +1 -0
- data/spec/fixtures/basic-tex/contents/contents.tex.should +1 -0
- data/spec/fixtures/basic-tex/contents/input_template.tex.erb +3 -0
- data/spec/fixtures/basic-tex/contents/input_template.tex.should +3 -0
- data/spec/fixtures/basic-tex/contents/unused_template.tex.erb +1 -0
- data/spec/fixtures/different-master-tex/contents/contents.tex.erb +1 -0
- data/spec/fixtures/different-master-tex/contents/contents.tex.should +1 -0
- data/spec/fixtures/different-master-tex/contents/master.tex.erb +9 -0
- data/spec/fixtures/different-master-tex/contents/master.tex.should +59 -0
- data/spec/fixtures/helper-methods-tex/contents/contents.tex.erb +7 -0
- data/spec/fixtures/helper-methods-tex/contents/contents.tex.should +7 -0
- data/spec/fixtures/helper-methods-tex/contents/input_template.tex.erb +5 -0
- data/spec/fixtures/helper-methods-tex/contents/input_template.tex.should +5 -0
- data/spec/fixtures/helper-methods-tex/contents/some_chapter/another_include.tex +1 -0
- data/spec/fixtures/helper-methods-tex/contents/some_chapter/test_include.tex.erb +7 -0
- data/spec/fixtures/helper-methods-tex/contents/some_chapter/test_include.tex.should +7 -0
- data/spec/fixtures/helper-methods-tex/contents/some_chapter.tex.erb +5 -0
- data/spec/fixtures/helper-methods-tex/contents/some_chapter.tex.should +5 -0
- data/spec/fixtures/helper-methods-tex/contents/template/helper/_some_partial.md.erb +1 -0
- data/spec/fixtures/helper-methods-tex/contents/template/helper/base.tex.erb +3 -0
- data/spec/fixtures/helper-methods-tex/contents/template/helper/base.tex.should +4 -0
- data/spec/fixtures/helper-methods-tex/contents/template/helper/info.md.erb +6 -0
- data/spec/fixtures/helper-methods-tex/contents/template/helper/info.tex.should +1 -0
- data/spec/fixtures/helper-methods-tex/contents/test_figure.tex.erb +1 -0
- data/spec/fixtures/helper-methods-tex/figures/test_figure.png +0 -0
- data/spec/fixtures/lib-helpers/contents/contents.tex.erb +3 -0
- data/spec/fixtures/lib-helpers/contents/contents.tex.should +3 -0
- data/spec/fixtures/lib-helpers/lib/helpers/document_helper.rb +7 -0
- data/spec/fixtures/lib-helpers/lib/init.rb +4 -0
- data/spec/fixtures/new/contents/contents.tex.erb +9 -0
- data/spec/fixtures/new/lib/helpers/document_helper.rb +11 -0
- data/spec/fixtures/new/lib/init.rb +4 -0
- data/spec/fixtures/prose/contents/chapter-1/01-some-scene.md +1 -0
- data/spec/fixtures/prose/contents/chapter-1/02-another-scene.md +1 -0
- data/spec/fixtures/prose/contents/chapter-1/03-and-another-scene.md +1 -0
- data/spec/fixtures/prose/contents/contents.md.erb +1 -0
- data/spec/fixtures/rerun/contents/contents.tex.erb +6 -0
- data/spec/fixtures/rerun/contents/contents.tex.should +4 -0
- data/spec/fixtures/texasrc/contents/contents.tex.erb +3 -0
- data/spec/fixtures/texasrc/contents/contents.tex.should +3 -0
- data/spec/spec_helper.rb +64 -0
- data/spec/texas/build_spec.rb +17 -0
- data/spec/texas/template/helper/info_spec.rb +41 -0
- data/spec/texas/template/runner_spec.rb +35 -0
- data/spec/texas/template_spec.rb +11 -0
- data/spec/texas_spec.rb +43 -0
- metadata +196 -0
data/bin/texas
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<%= page_break %>
|
|
2
|
+
|
|
3
|
+
<% if o.title.to_s.size > 0 %>
|
|
4
|
+
# <%= o.title %>
|
|
5
|
+
<% end %>
|
|
6
|
+
|
|
7
|
+
<% templates = o.templates || [] %>
|
|
8
|
+
|
|
9
|
+
<% templates.each_with_index do |template, index| %>
|
|
10
|
+
<%= render template %>
|
|
11
|
+
|
|
12
|
+
<%= scene_delimiter unless index == templates.size - 1 %>
|
|
13
|
+
<% end %>
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
\documentclass[a4paper,parskip=half*,12pt]{scrartcl}
|
|
2
|
+
|
|
3
|
+
\usepackage{relsize}
|
|
4
|
+
|
|
5
|
+
\usepackage{amsmath,amstext,amssymb}
|
|
6
|
+
|
|
7
|
+
\usepackage[ngerman]{babel}
|
|
8
|
+
\usepackage[utf8]{inputenc}
|
|
9
|
+
|
|
10
|
+
%\usepackage[paper=a4paper,left=40mm,right=40mm,top=25mm,bottom=25mm]{geometry}
|
|
11
|
+
|
|
12
|
+
\usepackage{courier}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
\usepackage[T1]{fontenc}
|
|
16
|
+
|
|
17
|
+
\usepackage[table]{xcolor}
|
|
18
|
+
\usepackage{color}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
\usepackage{url,hyperref}
|
|
22
|
+
\urlstyle{same}
|
|
23
|
+
|
|
24
|
+
\usepackage{setspace}
|
|
25
|
+
|
|
26
|
+
\clubpenalty = 10000
|
|
27
|
+
\widowpenalty = 10000
|
|
28
|
+
\displaywidowpenalty = 10000
|
|
29
|
+
|
|
30
|
+
\setlength{\parindent}{1em}
|
|
31
|
+
|
|
32
|
+
\setcounter{secnumdepth}{0}
|
|
33
|
+
\setcounter{tocdepth}{0}
|
|
34
|
+
|
|
35
|
+
\usepackage{fancyhdr}
|
|
36
|
+
\pagestyle{fancy}
|
|
37
|
+
|
|
38
|
+
\fancyhead{}
|
|
39
|
+
\fancyfoot{}
|
|
40
|
+
|
|
41
|
+
\fancyhead[L]{<%= document.author %> -- <%= document.title %>}
|
|
42
|
+
\fancyhead[C]{}
|
|
43
|
+
\fancyhead[R]{}
|
|
44
|
+
|
|
45
|
+
\fancyfoot[L]{}
|
|
46
|
+
\fancyfoot[C]{}
|
|
47
|
+
\fancyfoot[R]{\thepage}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
\onehalfspacing
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
require 'yaml'
|
|
2
|
+
|
|
3
|
+
module Texas
|
|
4
|
+
module Build
|
|
5
|
+
class Base
|
|
6
|
+
CONFIG_FILE = ".texasrc"
|
|
7
|
+
MASTER_TEMPLATE = "master.tex"
|
|
8
|
+
|
|
9
|
+
attr_reader :root, :options
|
|
10
|
+
attr_reader :master_file, :contents_dir, :contents_template
|
|
11
|
+
attr_reader :current_template, :ran_templates
|
|
12
|
+
|
|
13
|
+
def initialize(_options)
|
|
14
|
+
@options = _options
|
|
15
|
+
@root = options.work_dir
|
|
16
|
+
@contents_dir = options.contents_dir
|
|
17
|
+
@contents_template = options.contents_template
|
|
18
|
+
@master_file = File.join(__path__, MASTER_TEMPLATE)
|
|
19
|
+
|
|
20
|
+
verbose { "Starting #{self.class}" }
|
|
21
|
+
verbose { "[i] work_dir: #{options.work_dir}".dark }
|
|
22
|
+
verbose { "[i] contents_dir: #{@contents_dir}".dark }
|
|
23
|
+
verbose { "[i] contents_template: #{@contents_template}".dark }
|
|
24
|
+
verbose { "[i] build_path: #{@build_path}".dark }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def __path__
|
|
28
|
+
File.join(root, 'tmp', 'build')
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def store
|
|
32
|
+
@store ||= OpenStruct.new
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def current_template=(t)
|
|
36
|
+
@ran_templates ||= []
|
|
37
|
+
@ran_templates << t unless t.nil?
|
|
38
|
+
@current_template = t
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def document_struct
|
|
42
|
+
@document_struct ||= begin
|
|
43
|
+
hash = config["document"] || {}
|
|
44
|
+
if options.merge_config
|
|
45
|
+
hash.merge! config[options.merge_config]
|
|
46
|
+
end
|
|
47
|
+
OpenStruct.new(hash)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def config
|
|
52
|
+
@config ||= begin
|
|
53
|
+
filename = File.join(root, CONFIG_FILE)
|
|
54
|
+
File.exist?(filename) ? YAML.load_file(filename) : {}
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def dest_file
|
|
59
|
+
@dest_file ||= File.join(root, "bin", "#{Template.basename contents_template}.pdf")
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def run_build_tasks(*tasks)
|
|
63
|
+
tasks.flatten.each { |t| run_build_task t }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def run_build_task(klass)
|
|
67
|
+
klass = eval("::Texas::Build::Task::#{klass}") if [Symbol, String].include?(klass.class)
|
|
68
|
+
klass.new(self).run
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def run
|
|
72
|
+
run_build_tasks before_tasks, basic_tasks, after_tasks
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
%w(before basic after).each do |method|
|
|
76
|
+
class_eval <<-INSTANCE_EVAL
|
|
77
|
+
def #{method}_tasks
|
|
78
|
+
self.class.tasks(:#{method})
|
|
79
|
+
end
|
|
80
|
+
INSTANCE_EVAL
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
class << self
|
|
84
|
+
def run(options)
|
|
85
|
+
instance = self.new(options)
|
|
86
|
+
instance.run
|
|
87
|
+
instance
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def tasks(key)
|
|
91
|
+
@@tasks ||= {}
|
|
92
|
+
initialize_tasks if @@tasks[self.to_s].nil?
|
|
93
|
+
@@tasks[self.to_s][key] ||= []
|
|
94
|
+
@@tasks[self.to_s][key]
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def initialize_tasks
|
|
98
|
+
@@tasks[self.to_s] = {}
|
|
99
|
+
(@@tasks[self.superclass.to_s] || {}).each do |k, v|
|
|
100
|
+
@@tasks[self.to_s][k] = v.dup
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
%w(before basic after).each do |method|
|
|
105
|
+
class_eval <<-CLASS_EVAL
|
|
106
|
+
def prepend_#{method}_task(*_tasks)
|
|
107
|
+
tasks(:#{method}).unshift(*_tasks)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def append_#{method}_task(*_tasks)
|
|
111
|
+
tasks(:#{method}).concat(_tasks)
|
|
112
|
+
end
|
|
113
|
+
alias #{method}_task append_#{method}_task
|
|
114
|
+
CLASS_EVAL
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module Texas
|
|
2
|
+
module Build
|
|
3
|
+
module Task
|
|
4
|
+
class AddDefaultTemplatesToBuildPath < Base
|
|
5
|
+
def build_path
|
|
6
|
+
build.__path__
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def run
|
|
10
|
+
glob = File.join(Texas.texas_dir, Texas.contents_subdir_name, '*.*')
|
|
11
|
+
Dir[glob].each do |filename|
|
|
12
|
+
dest = File.join(build_path, File.basename(filename))
|
|
13
|
+
unless File.exists?(dest)
|
|
14
|
+
FileUtils.cp filename, build_path
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module Texas
|
|
2
|
+
module Build
|
|
3
|
+
module Task
|
|
4
|
+
class CopyContentsToBuildPath < Base
|
|
5
|
+
def run
|
|
6
|
+
ensure_build_path_exists
|
|
7
|
+
copy_to_build_path
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def build_path
|
|
11
|
+
build.__path__
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def contents_dir
|
|
15
|
+
build.contents_dir
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def ensure_build_path_exists
|
|
19
|
+
FileUtils.mkdir_p build_path
|
|
20
|
+
rescue
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def copy_to_build_path
|
|
24
|
+
FileUtils.rm_r build_path
|
|
25
|
+
FileUtils.cp_r contents_dir, build_path
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module Texas
|
|
2
|
+
module Build
|
|
3
|
+
module Task
|
|
4
|
+
class OpenPDF < Base
|
|
5
|
+
DEFAULT_OPEN_CMD = "evince"
|
|
6
|
+
|
|
7
|
+
def cmd
|
|
8
|
+
@cmd ||= scripts && scripts['open']
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def scripts
|
|
12
|
+
build.config['script']
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def run
|
|
16
|
+
return unless build.options.open_pdf
|
|
17
|
+
if open_pdf_cmd
|
|
18
|
+
system "#{open_pdf_cmd} #{build.dest_file}"
|
|
19
|
+
else
|
|
20
|
+
puts "Can't open PDF: no default command recognized. Specify in #{CONFIG_FILE}"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def open_pdf_cmd
|
|
25
|
+
if cmd
|
|
26
|
+
cmd
|
|
27
|
+
else
|
|
28
|
+
default = `which #{DEFAULT_OPEN_CMD}`.strip
|
|
29
|
+
default.empty? ? nil : default
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module Texas
|
|
2
|
+
module Build
|
|
3
|
+
module Task
|
|
4
|
+
class PublishPDF < Base
|
|
5
|
+
|
|
6
|
+
def master_file
|
|
7
|
+
build.master_file
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def build_path
|
|
11
|
+
build.__path__
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def dest_file
|
|
15
|
+
build.dest_file
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def run
|
|
19
|
+
run_pdflatex
|
|
20
|
+
copy_pdf_file_to_dest_dir
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def copy_pdf_file_to_dest_dir
|
|
24
|
+
tmp_file = File.join(build_path, "#{File.basename(master_file, '.tex')}.pdf")
|
|
25
|
+
FileUtils.mkdir_p File.dirname(dest_file)
|
|
26
|
+
FileUtils.copy tmp_file, dest_file
|
|
27
|
+
verbose {
|
|
28
|
+
file = File.join(build_path, "master.log")
|
|
29
|
+
output = `grep "Output written on" #{file}`
|
|
30
|
+
numbers = output.scan(/\((\d+?) pages\, (\d+?) bytes\)\./).flatten
|
|
31
|
+
@page_count = numbers.first.to_i
|
|
32
|
+
"Written PDF in #{dest_file.gsub(root, '')} (#{@page_count} pages)".green
|
|
33
|
+
}
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def run_pdflatex
|
|
37
|
+
verbose { "Running pdflatex in #{build_path} ..." }
|
|
38
|
+
Dir.chdir build_path
|
|
39
|
+
`pdflatex #{File.basename(master_file)}`
|
|
40
|
+
`pdflatex #{File.basename(master_file)}`
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module Texas
|
|
2
|
+
module Build
|
|
3
|
+
module Task
|
|
4
|
+
class RunBeforeScripts < Base
|
|
5
|
+
def cmd
|
|
6
|
+
@cmd ||= scripts && scripts['before']
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def scripts
|
|
10
|
+
build.config['script']
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def run
|
|
14
|
+
if cmd
|
|
15
|
+
verbose { "\n[i] Running before script:\n\n #{cmd.cyan}\n\n" }
|
|
16
|
+
system cmd
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module Texas
|
|
2
|
+
module Build
|
|
3
|
+
module Task
|
|
4
|
+
class RunMasterTemplate < Base
|
|
5
|
+
|
|
6
|
+
def find_master_template(possible_templates)
|
|
7
|
+
regexes = Template.handlers.keys
|
|
8
|
+
valid_master_templates = possible_templates.select do |f|
|
|
9
|
+
regexes.any? { |regex| f =~ regex }
|
|
10
|
+
end
|
|
11
|
+
valid_master_templates.first
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def run
|
|
15
|
+
filename = find_master_template Dir[build.master_file+'*']
|
|
16
|
+
master_template = Template.create(filename, build)
|
|
17
|
+
master_template.write
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
data/lib/texas/build.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Texas
|
|
2
|
+
module Build
|
|
3
|
+
end
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
require_relative 'build/base'
|
|
7
|
+
require_relative 'build/dry'
|
|
8
|
+
require_relative 'build/final'
|
|
9
|
+
|
|
10
|
+
require_relative 'build/task/base'
|
|
11
|
+
|
|
12
|
+
all_rbs = Dir[ File.join( File.dirname(__FILE__), "build", "task", "*.rb" ) ]
|
|
13
|
+
all_rbs.each do |t|
|
|
14
|
+
require t
|
|
15
|
+
end
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
require 'term/ansicolor'
|
|
2
|
+
|
|
3
|
+
class String
|
|
4
|
+
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
module Term
|
|
8
|
+
# The ANSIColor module can be used for namespacing and mixed into your own
|
|
9
|
+
# classes.
|
|
10
|
+
module NoColor
|
|
11
|
+
# :stopdoc:
|
|
12
|
+
ATTRIBUTES = [
|
|
13
|
+
[ :clear , 0 ], # String#clear is already used to empty string in Ruby 1.9
|
|
14
|
+
[ :reset , 0 ], # synonym for :clear
|
|
15
|
+
[ :bold , 1 ],
|
|
16
|
+
[ :dark , 2 ],
|
|
17
|
+
[ :italic , 3 ], # not widely implemented
|
|
18
|
+
[ :underline , 4 ],
|
|
19
|
+
[ :underscore , 4 ], # synonym for :underline
|
|
20
|
+
[ :blink , 5 ],
|
|
21
|
+
[ :rapid_blink , 6 ], # not widely implemented
|
|
22
|
+
[ :negative , 7 ], # no reverse because of String#reverse
|
|
23
|
+
[ :concealed , 8 ],
|
|
24
|
+
[ :strikethrough , 9 ], # not widely implemented
|
|
25
|
+
[ :black , 30 ],
|
|
26
|
+
[ :red , 31 ],
|
|
27
|
+
[ :green , 32 ],
|
|
28
|
+
[ :yellow , 33 ],
|
|
29
|
+
[ :blue , 34 ],
|
|
30
|
+
[ :magenta , 35 ],
|
|
31
|
+
[ :cyan , 36 ],
|
|
32
|
+
[ :white , 37 ],
|
|
33
|
+
[ :on_black , 40 ],
|
|
34
|
+
[ :on_red , 41 ],
|
|
35
|
+
[ :on_green , 42 ],
|
|
36
|
+
[ :on_yellow , 43 ],
|
|
37
|
+
[ :on_blue , 44 ],
|
|
38
|
+
[ :on_magenta , 45 ],
|
|
39
|
+
[ :on_cyan , 46 ],
|
|
40
|
+
[ :on_white , 47 ],
|
|
41
|
+
[ :intense_black , 90 ], # High intensity, aixterm (works in OS X)
|
|
42
|
+
[ :intense_red , 91 ],
|
|
43
|
+
[ :intense_green , 92 ],
|
|
44
|
+
[ :intense_yellow , 93 ],
|
|
45
|
+
[ :intense_blue , 94 ],
|
|
46
|
+
[ :intense_magenta , 95 ],
|
|
47
|
+
[ :intense_cyan , 96 ],
|
|
48
|
+
[ :intense_white , 97 ],
|
|
49
|
+
[ :on_intense_black , 100 ], # High intensity background, aixterm (works in OS X)
|
|
50
|
+
[ :on_intense_red , 101 ],
|
|
51
|
+
[ :on_intense_green , 102 ],
|
|
52
|
+
[ :on_intense_yellow , 103 ],
|
|
53
|
+
[ :on_intense_blue , 104 ],
|
|
54
|
+
[ :on_intense_magenta , 105 ],
|
|
55
|
+
[ :on_intense_cyan , 106 ],
|
|
56
|
+
[ :on_intense_white , 107 ]
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
ATTRIBUTE_NAMES = ATTRIBUTES.transpose.first
|
|
60
|
+
# :startdoc:
|
|
61
|
+
|
|
62
|
+
# Returns true if Term::ANSIColor supports the +feature+.
|
|
63
|
+
#
|
|
64
|
+
# The feature :clear, that is mixing the clear color attribute into String,
|
|
65
|
+
# is only supported on ruby implementations, that do *not* already
|
|
66
|
+
# implement the String#clear method. It's better to use the reset color
|
|
67
|
+
# attribute instead.
|
|
68
|
+
def support?(feature)
|
|
69
|
+
case feature
|
|
70
|
+
when :clear
|
|
71
|
+
!String.instance_methods(false).map(&:to_sym).include?(:clear)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
# Returns true, if the coloring function of this module
|
|
75
|
+
# is switched on, false otherwise.
|
|
76
|
+
def self.coloring?
|
|
77
|
+
@coloring
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Turns the coloring on or off globally, so you can easily do
|
|
81
|
+
# this for example:
|
|
82
|
+
# Term::ANSIColor::coloring = STDOUT.isatty
|
|
83
|
+
def self.coloring=(val)
|
|
84
|
+
@coloring = val
|
|
85
|
+
end
|
|
86
|
+
self.coloring = true
|
|
87
|
+
|
|
88
|
+
ATTRIBUTES.each do |c, v|
|
|
89
|
+
eval <<-EOT
|
|
90
|
+
def #{c}(string = nil)
|
|
91
|
+
result = ''
|
|
92
|
+
if block_given?
|
|
93
|
+
result << yield
|
|
94
|
+
elsif string.respond_to?(:to_str)
|
|
95
|
+
result << string.to_str
|
|
96
|
+
elsif respond_to?(:to_str)
|
|
97
|
+
result << to_str
|
|
98
|
+
else
|
|
99
|
+
return result #only switch on
|
|
100
|
+
end
|
|
101
|
+
result
|
|
102
|
+
end
|
|
103
|
+
EOT
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Regular expression that is used to scan for ANSI-sequences while
|
|
107
|
+
# uncoloring strings.
|
|
108
|
+
COLORED_REGEXP = /\e\[(?:(?:[349]|10)[0-7]|[0-9])?m/
|
|
109
|
+
|
|
110
|
+
# Returns an uncolored version of the string, that is all
|
|
111
|
+
# ANSI-sequences are stripped from the string.
|
|
112
|
+
def uncolored(string = nil) # :yields:
|
|
113
|
+
if block_given?
|
|
114
|
+
yield.to_str.gsub(COLORED_REGEXP, '')
|
|
115
|
+
elsif string.respond_to?(:to_str)
|
|
116
|
+
string.to_str.gsub(COLORED_REGEXP, '')
|
|
117
|
+
elsif respond_to?(:to_str)
|
|
118
|
+
to_str.gsub(COLORED_REGEXP, '')
|
|
119
|
+
else
|
|
120
|
+
''
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
module_function
|
|
125
|
+
|
|
126
|
+
# Returns an array of all Term::ANSIColor attributes as symbols.
|
|
127
|
+
def attributes
|
|
128
|
+
ATTRIBUTE_NAMES
|
|
129
|
+
end
|
|
130
|
+
extend self
|
|
131
|
+
end
|
|
132
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require_relative 'core_ext/string'
|