lib/polytexnic/literal.rb in polytexnic-0.9.9 vs lib/polytexnic/literal.rb in polytexnic-0.9.10

- old
+ new

@@ -4,19 +4,10 @@ # Matches the line for syntax highlighting. # %= lang: <language>[, options: ...] LANG_REGEX = /^\s*%=\s+lang:\s*(\w+)(?:,\s*options:(.*))?/ - # Matches the line for code inclusion. - # %= <</path/to/code.ext - CODE_INCLUSION_REGEX = /^\s*%=\s+<<\s*\( # opening - \s*([^\s]+?) # path to file - (?:\[(.+?)\])? # optional section name - (?:,\s*lang:\s*(\w+))? # optional lang - (,\s*options:\s*.*)? # optional options - \s*\) # closing paren - /x # Makes the caches for literal environments. def cache_literal(polytex, format = :html) output = [] lines = polytex.split("\n") @@ -72,24 +63,21 @@ in_codelisting = true output << line elsif line =~ /\s*\\end\{codelisting\}/ && !in_verbatim in_codelisting = false output << line - elsif line =~ CODE_INCLUSION_REGEX && !in_verbatim + elsif (included_code = CodeInclusion::Code.for(line)) && !in_verbatim # Reduce to a previously solved problem. # We transform # %= <<(/path/to/file.rb) # to # %= lang:rb # \begin{code} # <content of file or section.rb> # \end{code} # and then prepend the code to the current `lines` array. - filename, sectionname, custom_language, highlight_options = $1, $2, $3, $4 - if filename - lines.unshift(*include_code(filename, sectionname, custom_language, highlight_options)) - end + lines.unshift(*included_code.to_s) elsif line.begin_literal? in_verbatim = true literal_type = line.literal_type skip = line.math_environment? || latex if line.math_environment? && !latex @@ -158,38 +146,10 @@ output << line end end end - # Returns the marked up file or section to be included, - # or an error message if file or section does not exist. - def include_code(filename, sectionname, custom_language, highlight_options) - reader = (sectionname ? IncludedSectionReader : IncludedFileReader).new - lang = "#{code_language(filename, custom_language)}#{highlight_options}" - code = ["%= lang:#{lang}"] - code << '\begin{code}' - code.concat(reader.read(filename, sectionname)) - code << '\end{code}' - - rescue FileNotFound => e - code_error("File '#{e.message}' does not exist") - rescue SectionNotFound => e - msg = e.message - err = "Could not find section header '#{msg}' in file '#{filename}'" - code_error(err) - end - - def code_error(details) - "\\verb+ERROR: #{details}+" - end - - def code_language(filename, custom_language) - extension_array = File.extname(filename).scan(/\.(.*)/).first - lang_from_extension = extension_array.nil? ? nil : extension_array[0] - language = custom_language || lang_from_extension || 'text' - end - # Returns a permanent salt for the syntax highlighting cache. def code_salt 'fbbc13ed4a51e27608037365e1d27a5f992b6339' end @@ -286,65 +246,13 @@ 'equation' else literal_type end end - - - class FileNotFound < Exception; end; - class IncludedFileReader - def read(filename, _) - raise(FileNotFound, filename) unless File.exist?(filename) - File.read(filename).split("\n") - end - end - - class SectionNotFound < Exception; end; - class IncludedSectionReader < IncludedFileReader - attr_reader :lines, :sectionname - - def read(filename, sectionname) - @lines = super - @sectionname = sectionname - - raise(SectionNotFound, section_begin_text) unless exist? - lines.slice(index_of_first_line, length) - end - - private - def exist? - !!index_of_section_begin - end - - def index_of_section_begin - @section_begin_i ||= lines.index(section_begin_text) - end - - def index_of_first_line - @first_line_i ||= index_of_section_begin + 1 - end - - def length - lines.slice(index_of_first_line, lines.size).index(section_end_text) - end - - def marker - '#//' - end - - def section_begin_text - "#{marker} begin #{sectionname}" - end - - def section_end_text - "#{marker} end" - end - end end end - class String include Polytexnic::Literal # Returns true if self matches \begin{...} where ... is a literal environment. # Note: Support for the 'metacode' environment exists solely to allow @@ -389,5 +297,6 @@ Polytexnic::Literal.math_environments.map do |s| Regexp.escape(s) end.join('|') end end +