Class: Codex::CodeFilter::CodeFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/codex/filters/code_filter.rb

Instance Attribute Summary

Attributes inherited from Filter

#tag

Instance Method Summary collapse

Methods inherited from Filter

tag

Instance Method Details

#filter_inline(text, args) ⇒ Object



49
50
51
52
# File 'lib/codex/filters/code_filter.rb', line 49

def filter_inline(text,args)
  desc = Descriptor.new(args)
  format_code(desc, text)
end

#filter_single(args) ⇒ Object



43
44
45
46
47
# File 'lib/codex/filters/code_filter.rb', line 43

def filter_single(args)
  desc = Descriptor.new(args)
  content = find_content_from(desc)
  format_code(desc, content)
end

#find_content_from(desc) ⇒ Object



69
70
71
# File 'lib/codex/filters/code_filter.rb', line 69

def find_content_from(desc)
  find_part_in(File.read(desc.file_name), desc.part)
end

#find_part_in(content, part_name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/codex/filters/code_filter.rb', line 73

def find_part_in(content, part_name)
  result = []
  state = part_name ? :skipping : :normal
  content.each_line do |line|
    if line.sub!(/(START|END):(\w+)/, '')
      if $2 == part_name
        if $1 == "START"
          state = :normal
        else
          state = :skipping
        end
      end
      next
    end
    result << line unless state == :skipping
  end
  result.join
end

#format_code(desc, code) ⇒ Object



54
55
56
57
58
59
# File 'lib/codex/filters/code_filter.rb', line 54

def format_code(desc, code)
  %{<div class="#{desc.css_class}">\n} +
  %{\n<pre name="code" class="#{desc.lang}:nogutter:nocontrols">#{code}} +
  %{</pre></div>} +
  format_link(desc)
end


61
62
63
64
65
66
67
# File 'lib/codex/filters/code_filter.rb', line 61

def format_link(desc)
  if desc.file_name.nil?
    "\n\n"
  else
    %{<div class="codeurl"><a href="txmt://open?url=file://#{File.expand_path(desc.file_name)}">#{desc.file_name}</a></div>\n\n}        
  end
end