Class: Riven::MarkupFile

Inherits:
Object
  • Object
show all
Defined in:
lib/riven/markup_file.rb

Overview

Represents a MD File

Instance Attribute Summary collapse

Instance Method Summary collapse

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.expand_path(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

#dirnameObject (readonly)

Returns the value of attribute dirname.



9
10
11
# File 'lib/riven/markup_file.rb', line 9

def dirname
  @dirname
end

#markupObject (readonly)

Returns the value of attribute markup.



9
10
11
# File 'lib/riven/markup_file.rb', line 9

def markup
  @markup
end

#pathObject (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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/riven/markup_file.rb', line 75

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



90
91
92
93
94
# File 'lib/riven/markup_file.rb', line 90

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

#resolve_includesObject

Recursive replace all includes with their respective file content



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/riven/markup_file.rb', line 40

public def resolve_includes
  loop do
    non_found = true

    @markup.gsub!(/<<\[\s*([^\]\s]+)\s*\]/) do |inc|
      non_found = false
      MarkupFile.new(@dirname.to_s + '/' + $1).markup
    end

    break if non_found
  end
end

#rewrite_pathsObject

Prefixes all paths with the file basedir in order to make everything working across includes over different directories



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/riven/markup_file.rb', line 59

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