Class: Gimli::MarkupFile

Inherits:
Object
  • Object
show all
Defined in:
lib/gimli/markupfile.rb

Overview

Class used to load files and determine if they are valid

Constant Summary collapse

FORMATS =

Accepted formats

[:markdown, :textile, :rdoc, :org, :creole, :rest, :asciidoc, :pod, :roff, :mediawiki]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ MarkupFile

Initializes the file object. Only reads contents if it’s a valid file



13
14
15
16
17
18
19
# File 'lib/gimli/markupfile.rb', line 13

def initialize(filename)
  @filename = filename
  extension = ::File.extname(@filename)
  @format = load_format(extension)
  @name = ::File.basename(@filename, extension)
  @data = ::File.open(@filename, 'rb') { |f| f.read } if valid? && ::File.exists?(@filename)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/gimli/markupfile.rb', line 7

def data
  @data
end

#filenameObject (readonly)

Returns the value of attribute filename.



7
8
9
# File 'lib/gimli/markupfile.rb', line 7

def filename
  @filename
end

#formatObject (readonly)

Returns the value of attribute format.



7
8
9
# File 'lib/gimli/markupfile.rb', line 7

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/gimli/markupfile.rb', line 7

def name
  @name
end

Instance Method Details

#load_format(format) ⇒ Symbol|nil

Converts the format to a symbol if it’s a valid format nil otherwise

Parameters:

  • format (String)

Returns:

  • (Symbol|nil)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gimli/markupfile.rb', line 30

def load_format(format)
  case format.to_s
    when /(md|mkdn?|mdown|markdown)$/i
      :markdown
    when /(textile)$/i
      :textile
    when /(rdoc)$/i
      :rdoc
    when /(org)$/i
      :org
    when /(creole)$/i
      :creole
    when /(re?st(\.txt)?)$/i
      :rest
    when /(asciidoc)$/i
      :asciidoc
    when /(pod)$/i
      :pod
    when /(\d)$/i
      :roff
    when /(media)?wiki$/i
      :mediawiki
    else
      nil
  end
end

#valid?Boolean

Is the file valid

Returns:

  • (Boolean)


23
24
25
# File 'lib/gimli/markupfile.rb', line 23

def valid?
  valid_format? @format
end

#valid_format?(format) ⇒ Boolean

Checks if the format is a valid one

Parameters:

  • format (String)

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/gimli/markupfile.rb', line 60

def valid_format?(format)
  return false if format.nil?

  FORMATS.include? format.to_sym
end