Class: Riven::MarkupFile
- Inherits:
-
Object
- Object
- Riven::MarkupFile
- Defined in:
- lib/riven/markup_file.rb
Overview
Represents a MD File
Instance Attribute Summary collapse
-
#dirname ⇒ Object
readonly
Returns the value of attribute dirname.
-
#markup ⇒ Object
readonly
Returns the value of attribute markup.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path) ⇒ MarkupFile
constructor
Constructor Also checks if the file exists.
- #read_all(markup_files, except = []) ⇒ Object
- #read_cover(cover_file) ⇒ Object
-
#remove_escape_sequences(markup) ⇒ Object
Removes the internal escape sequence.
-
#resolve_includes ⇒ Object
Recursive replace all includes with their respective file content.
-
#rewrite_paths ⇒ Object
Prefixes all paths with the file basedir in order to make everything working across includes over different directories.
Constructor Details
#initialize(path) ⇒ MarkupFile
Constructor Also checks if the file exists. If not, an exception will be thrown.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/riven/markup_file.rb', line 17 public def initialize(path) @path = File.(path) @dirname = Pathname.new(path).dirname unless File.exists?(@path) raise "File '#{path}' doesn't exist" end if File.directory?(@path) raise "Mixing files and directories is not allowed, sorry" end puts "Loading file: " + path @markup = "\n" + File.read(@path) rewrite_paths resolve_includes end |
Instance Attribute Details
#dirname ⇒ Object (readonly)
Returns the value of attribute dirname.
9 10 11 |
# File 'lib/riven/markup_file.rb', line 9 def dirname @dirname end |
#markup ⇒ Object (readonly)
Returns the value of attribute markup.
9 10 11 |
# File 'lib/riven/markup_file.rb', line 9 def markup @markup end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/riven/markup_file.rb', line 9 def path @path end |
Instance Method Details
#read_all(markup_files, except = []) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/riven/markup_file.rb', line 84 public def read_all(markup_files, except = []) markup = '' markup_files = [markup_files] unless markup_files.respond_to?(:each) except = [except] unless except.respond_to?(:each) markup_files.each do |file| unless exclude?(except, file) markup << "\n" + file.markup end end return markup end |
#read_cover(cover_file) ⇒ Object
99 100 101 102 103 |
# File 'lib/riven/markup_file.rb', line 99 public def read_cover(cover_file) cover_markup = "\n////[COVERSTART]////\n" cover_markup << Riven::MarkupFile.read_all(cover_file) cover_markup << "\n////[COVEREND]////\n" end |
#remove_escape_sequences(markup) ⇒ Object
Removes the internal escape sequence
114 115 116 |
# File 'lib/riven/markup_file.rb', line 114 public def remove_escape_sequences(markup) markup.gsub('¦', '') end |
#resolve_includes ⇒ Object
Recursive replace all includes with their respective file content
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/riven/markup_file.rb', line 40 public def resolve_includes loop do non_found = true @markup.gsub!(/(.)?<<\[\s*([^\]\s]+)\s*\]/) do |inc| if $1 == '_' # special secret escape sign, will be removed afterwards. # Required to avoid recursive substitution inc.gsub!('_', '¦') inc elsif $1 == '¦' inc else non_found = false ($1 ? $1 : '') + MarkupFile.new(@dirname.to_s + '/' + $2).markup end end break if non_found end end |
#rewrite_paths ⇒ Object
Prefixes all paths with the file basedir in order to make everything working across includes over different directories
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/riven/markup_file.rb', line 68 public def rewrite_paths @markup.gsub!(/\[([^\]]+)\]\(([^\)]+)\)/) do |match| label = $1 ref = $2 if ref =~ /^http(s)?/ "[#{label}](#{ref})" else puts " - Rewriting ref '#{ref}' to '#{@dirname}/#{ref}'" "[#{label}](#{@dirname}/#{ref})" end end end |