Class: Codex::Content
- Inherits:
-
Object
- Object
- Codex::Content
- Defined in:
- lib/codex/content.rb
Defined Under Namespace
Classes: Descriptor
Constant Summary collapse
- START_SLIDE =
%{<div class="slide">\n}- END_SLIDE =
%{</div>\n}- BETWEEN_SLIDES =
END_SLIDE + "\n" + START_SLIDE
- CODE_PATTERN =
/^:code\s+(.+)/- INCLUDED_CODE_PREFIX =
Temporarily prepended to lines of incldued code to stop them being processed as Textfile
"XXX\001"- INCLUDED_CODE_PREFIX_REGEXP =
/#{INCLUDED_CODE_PREFIX}/mo
Instance Method Summary collapse
- #find_content_from(desc) ⇒ Object
- #find_part_in(content, part_name) ⇒ Object
- #format_code(desc, code) ⇒ Object
-
#initialize(original) ⇒ Content
constructor
A new instance of Content.
-
#preprocess_code(text) ⇒ Object
look for :code filename and substitute in the appropriate part of the given file.
-
#preprocess_inlinecode(text) ⇒ Object
Look for :inlinecode /…/:endinlinecode and substitute in as if it came from a file.
- #remove_code_escaping_from(html) ⇒ Object
- #split_into_slides(textile) ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(original) ⇒ Content
Returns a new instance of Content.
57 58 59 |
# File 'lib/codex/content.rb', line 57 def initialize(original) @original = original.sub(/__END__.*/m, '').gsub(/__SKIP__.*?__ENDSKIP__/m, '') end |
Instance Method Details
#find_content_from(desc) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/codex/content.rb', line 119 def find_content_from(desc) begin content = File.read(desc.file_name) rescue Exception => e STDERR.puts e. exit 2 end find_part_in(content, desc.part) end |
#find_part_in(content, part_name) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/codex/content.rb', line 130 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
149 150 151 152 153 154 155 |
# File 'lib/codex/content.rb', line 149 def format_code(desc, code) code = code.gsub(/^/m, INCLUDED_CODE_PREFIX) %{<div class="#{desc.css_class}">\n} + %{\n<pre name="code" class="#{desc.lang}:nogutter:nocontrols">#{code}} + %{</pre></div>} + %{<div class="codeurl"><a href="txmt://open?url=file://#{File.(desc.file_name)}">#{desc.file_name}</a></div>\n\n} end |
#preprocess_code(text) ⇒ Object
look for :code filename and substitute in the appropriate part of the given file. Do in two steps because the regexp engine isn;t reentrant
71 72 73 74 75 76 77 78 |
# File 'lib/codex/content.rb', line 71 def preprocess_code(text) chunks = {} text.scan(CODE_PATTERN) do |file,| desc = Descriptor.new(file) chunks[file] = find_content_from(desc) end text.gsub(CODE_PATTERN) { file = $1.dup; format_code(Descriptor.new(file), chunks[file]) } end |
#preprocess_inlinecode(text) ⇒ Object
Look for :inlinecode /…/:endinlinecode and substitute in as if it came from a file
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/codex/content.rb', line 81 def preprocess_inlinecode(text) state = :copying result = [] text.split(/\n/).each do |line| case state when :copying if line =~ /^:inlinecode(.*)/ args = $1.strip desc = Descriptor.new("--[#{args}]") result << %{<div class="#{desc.css_class}">\n} result << %{\n<pre name="code" class="#{desc.lang}:nogutter:nocontrols">\n} state = :incode else result << line end when :incode if line =~ /^:endinlinecode/ result << "</pre></div>\n\n" state = :copying else result << INCLUDED_CODE_PREFIX + line end end end result.join("\n") end |
#remove_code_escaping_from(html) ⇒ Object
157 158 159 |
# File 'lib/codex/content.rb', line 157 def remove_code_escaping_from(html) html.gsub(INCLUDED_CODE_PREFIX_REGEXP, '') end |
#split_into_slides(textile) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/codex/content.rb', line 109 def (textile) result = [] = textile.split(/^h1/).each do || unless .empty? result << START_SLIDE << "\nh1" << << END_SLIDE end end result.join end |
#to_html ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/codex/content.rb', line 61 def to_html textile = preprocess_inlinecode(@original) textile = preprocess_code(textile) content = (textile) html = RedCloth.new(content).to_html remove_code_escaping_from(html) end |