texas 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,6 +24,7 @@ module Texas
24
24
  options.load_local_libs = true
25
25
  options.contents_dir = Texas.contents_subdir_name
26
26
  options.contents_template = find_contents_file("contents")
27
+ options.backtrace = false
27
28
  options.colors = true
28
29
  options.merge_config = nil
29
30
  options.verbose = false
@@ -71,17 +72,18 @@ module Texas
71
72
  opts.separator ""
72
73
  opts.separator "Common options:"
73
74
 
74
- # Boolean switch.
75
+ opts.on("--[no-]backtrace", "Switch backtrace") do |v|
76
+ options.backtrace = v
77
+ end
78
+
75
79
  opts.on("-c", "--[no-]color", "Switch colors") do |v|
76
80
  options.colors = v
77
81
  end
78
82
 
79
- # Boolean switch.
80
83
  opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
81
84
  options.verbose = v
82
85
  end
83
86
 
84
- # Boolean switch.
85
87
  opts.on("-w", "--[no-]warnings", "Switch warnings") do |v|
86
88
  options.warnings = v
87
89
  end
@@ -37,9 +37,20 @@ module Texas
37
37
  Texas.warnings = @options.warnings
38
38
  load_local_libs if @options.load_local_libs
39
39
  @task_instance = task_class.new(@options)
40
- @task_instance.run
40
+ run
41
41
  end
42
42
 
43
+ # Display the error message that caused the exception.
44
+ def display_error_message(ex)
45
+ puts "#{@options.task} aborted!"
46
+ puts ex.message
47
+ if @options.backtrace
48
+ puts ex.backtrace
49
+ else
50
+ puts "(See full trace with --backtrace)"
51
+ end
52
+ end
53
+
43
54
  def extend_string_class
44
55
  mod = @options.colors ? Term::ANSIColor : Term::NoColor
45
56
  String.send :include, mod
@@ -50,6 +61,13 @@ module Texas
50
61
  require init_file if File.exist?(init_file)
51
62
  end
52
63
 
64
+ def run
65
+ @task_instance.run
66
+ rescue Exception => ex
67
+ display_error_message ex
68
+ exit 1
69
+ end
70
+
53
71
  def task_class
54
72
  map = {
55
73
  :build => Build::Final,
@@ -12,10 +12,6 @@ module Texas
12
12
  @build ||= klass.run(options)
13
13
  end
14
14
 
15
- def track
16
- build.track
17
- end
18
-
19
15
  def run
20
16
  end
21
17
  end
@@ -60,3 +60,4 @@ Texas::Template.known_extensions = []
60
60
 
61
61
  require_relative 'template/helper'
62
62
  require_relative 'template/runner'
63
+ require_relative 'template/template_error'
@@ -57,7 +57,7 @@ module Texas
57
57
  if filename = find_template_file(parts, possible_exts, possible_paths)
58
58
  filename
59
59
  else
60
- raise "File doesnot exists anywhere: #{parts.inspect}#{possible_exts.inspect} in #{possible_paths.inspect} #{}"
60
+ raise TemplateError.new(self, "File doesnot exists anywhere: #{parts.size > 1 ? parts : parts.first}")
61
61
  end
62
62
  end
63
63
 
@@ -69,9 +69,25 @@ module Texas
69
69
 
70
70
  # Renders a template with the given locals.
71
71
  #
72
- def render(name, locals = {})
73
- template_file = find_template_file!([name], template_extensions)
74
- Texas::Template.create(template_file, build).__run__(locals)
72
+ def render(options, locals = {})
73
+ if [String, Symbol].include?(options.class)
74
+ options = {:templates => [options]}
75
+ end
76
+ if name = options[:template]
77
+ options[:templates] = [name]
78
+ end
79
+ if glob = options[:glob]
80
+ options[:templates] = templates_by_glob(glob)
81
+ end
82
+ options[:locals] = locals unless locals.empty?
83
+ render_as_array(options).join(options[:join].to_s)
84
+ end
85
+
86
+ def render_as_array(options)
87
+ options[:templates].map do |name|
88
+ template_file = find_template_file!([name], template_extensions)
89
+ Texas::Template.create(template_file, build).__run__(options[:locals])
90
+ end
75
91
  end
76
92
 
77
93
  # Returns all extensions the Template::Runner can handle.
@@ -46,6 +46,10 @@ module Texas
46
46
  def __render__(locals = {})
47
47
  @locals = OpenStruct.new(locals)
48
48
  ERB.new(@content, nil, nil, "@erbout").result(binding)
49
+ rescue TemplateError => ex
50
+ raise ex
51
+ rescue Exception => ex
52
+ raise TemplateError.new(self, ex.message, ex)
49
53
  end
50
54
 
51
55
  def __run__(locals = {})
@@ -0,0 +1,34 @@
1
+ class TemplateError < StandardError
2
+ attr_accessor :original, :template
3
+
4
+ def initialize(template, message, original = nil)
5
+ super(message)
6
+ self.template = template
7
+ self.original = original
8
+ end
9
+
10
+ def filename
11
+ template.filename.gsub(template.build_path, '')
12
+ end
13
+
14
+ def parse_backtrace_for_origin
15
+ arr = original ? original.backtrace : backtrace
16
+ arr.each_with_index do |line, index|
17
+ if line =~ /\(erb\):(\d+)/
18
+ @line_number = $1.to_i
19
+ line_before = backtrace[index-1]
20
+ @method_name = line_before =~ /\d+:in\s+`([^\)]+)'/ && $1
21
+ return
22
+ end
23
+ end
24
+ end
25
+
26
+ def origin
27
+ parse_backtrace_for_origin
28
+ "#{filename}:#{@line_number}:in `#{@method_name}'"
29
+ end
30
+
31
+ def message
32
+ super + "\n#{origin}".cyan
33
+ end
34
+ end
@@ -2,7 +2,7 @@ module Texas
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- BUILD = 4
5
+ BUILD = 5
6
6
 
7
7
  STRING = [MAJOR, MINOR, BUILD].join('.').freeze
8
8
  end
@@ -2,6 +2,12 @@ Should find this template relatively and render the template another\_include be
2
2
 
3
3
  <%= render "another_include" %>
4
4
 
5
+ <%= render :template => "another_include" %>
6
+
7
+ <%= render :templates => ["another_include"] %>
8
+
9
+ <%= render :glob => "another_*" %>
10
+
5
11
  When using the LaTeX input command, the path should be relative to master.tex e.g. some\_chapter/another\_include
6
12
 
7
13
  <%= input "another_include" %>
@@ -2,6 +2,12 @@ Should find this template relatively and render the template another\_include be
2
2
 
3
3
  This file is a static .tex file.
4
4
 
5
+ This file is a static .tex file.
6
+
7
+ This file is a static .tex file.
8
+
9
+ This file is a static .tex file.
10
+
5
11
  When using the LaTeX input command, the path should be relative to master.tex e.g. some\_chapter/another\_include
6
12
 
7
13
  \input{some_chapter/another_include}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: texas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-15 00:00:00.000000000 Z
12
+ date: 2013-04-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: term-ansicolor
@@ -72,6 +72,7 @@ files:
72
72
  - lib/texas/template/helper/info.rb
73
73
  - lib/texas/template/helper/base.rb
74
74
  - lib/texas/template/runner.rb
75
+ - lib/texas/template/template_error.rb
75
76
  - lib/texas/template/runner/md.rb
76
77
  - lib/texas/template/runner/tex.rb
77
78
  - lib/texas/template/runner/base.rb