Class: Dsl::Latex::Simple

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl-latex-simple/simple.rb,
lib/dsl-latex-simple/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSimple

Returns a new instance of Simple.



7
8
9
# File 'lib/dsl-latex-simple/simple.rb', line 7

def initialize()
  @out = ''
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dsl-latex-simple/simple.rb', line 59

def tag(tagname, *name, &block)
  if tagname == :start
    tagname = :begin
  end
  macro_begin(name, tagname, block_given?)
  if block_given?
    # create a new object and translate it
    # then concat its output
    aux = Dsl::Latex::Simple.new
    aux.instance_eval &block
    content = aux.out
    if content
      @out << content.to_s 
    end
    macro_end(name, tagname)
  end
  nil
end

Instance Attribute Details

#outObject

Returns the value of attribute out.



5
6
7
# File 'lib/dsl-latex-simple/simple.rb', line 5

def out
  @out
end

Class Method Details

.generate(path) ⇒ Object



76
77
78
79
80
81
# File 'lib/dsl-latex-simple/simple.rb', line 76

def self.generate(path)
  s = File.read(path)
  x = Dsl::Latex::Simple.new 
  x.instance_eval(s, path, 1)
  puts x.out
end

Instance Method Details

#c(*x) ⇒ Object



72
73
74
# File 'lib/dsl-latex-simple/simple.rb', line 72

def c(*x)
  @out << x.join("\n")+"\n"
end

#esc(s) ⇒ Object



61
62
63
64
# File 'lib/dsl-latex-simple/simple.rb', line 61

def esc(s)
  s.gsub!(%q{\\},%q{\\\\})
  s
end

#highlight_code(language, code) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dsl-latex-simple/simple.rb', line 83

def highlight_code(language, code)
  start %q{rawhtml} do
    hl = Simplabs::Highlight.highlight(language, code)
    c "<pre>\n#{hl}\n</pre>\n"
  end

  start %q{latexonly} do
    start :lstlisting, %Q{language=#{language}} do
      c code
    end
  end
end

#macro_begin(name, tagname, block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dsl-latex-simple/simple.rb', line 11

def macro_begin(name, tagname, block)
  @out << "\\#{tagname}"
  @out << '{' if block and tagname != :begin
  name.each do |n|
    if n.is_a? Array
      @out << "["+n.join('][')+"]"
    else
      @out << "{#{n}}"
    end
  end
  @out << "\n"
end

#macro_end(name, tagname) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dsl-latex-simple/simple.rb', line 24

def macro_end(name, tagname)
  if tagname == :begin
    @out << "\\end{#{name[0]}}" 
  else
    @out << '}'
    name.each do |n|
      if n.is_a? Array
        @out << "["+n.join('][')+"]"
      else
        @out << "{#{n}}"
      end
    end
  end
  @out << "\n"
end

#tag(tagname, *name, &block) ⇒ Object Also known as: method_missing



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dsl-latex-simple/simple.rb', line 40

def tag(tagname, *name, &block)
  if tagname == :start
    tagname = :begin
  end
  macro_begin(name, tagname, block_given?)
  if block_given?
    # create a new object and translate it
    # then concat its output
    aux = Dsl::Latex::Simple.new
    aux.instance_eval &block
    content = aux.out
    if content
      @out << content.to_s 
    end
    macro_end(name, tagname)
  end
  nil
end

#trim(s) ⇒ Object



66
67
68
69
70
# File 'lib/dsl-latex-simple/simple.rb', line 66

def trim(s)
  s.gsub!(/^\s+/,'')
  s.gsub!(/\s+$/,'')
  s
end