Class: Showoff::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/showoff/config.rb

Class Method Summary collapse

Class Method Details

.expand_sectionsObject

Expand and normalize all the different variations that the sections structure can exist in. When finished, this should return an ordered hash of one or more section titles pointing to an array of filenames, for example:

"Section name": [ "array.md, "of.md, "files.md"],
"Another Section": [ "two/array.md, "two/of.md, "two/files.md"],

See valid input forms at

https://puppetlabs.github.io/showoff/documentation/PRESENTATION_rdoc.html#label-Defining+slides+using+the+sections+setting.

Source:

https://github.com/puppetlabs/showoff/blob/3f43754c84f97be4284bb34f9bc7c42175d45226/lib/showoff_utils.rb#L427-L475


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/showoff/config.rb', line 63

def self.expand_sections
  begin
    if @@config.is_a?(Hash)
      # dup so we don't overwrite the original data structure and make it impossible to re-localize
      sections = @@config['sections'].dup
    else
      sections = @@config.dup
    end

    if sections.is_a? Array
      sections = self.legacy_sections(sections)
    elsif sections.is_a? Hash
      raise "Named sections are unsupported on Ruby versions less than 1.9." if RUBY_VERSION.start_with? '1.8'
      sections.each do |key, value|
        next if value.is_a? Array
        path = File.dirname(value)
        data = JSON.parse(File.read(File.join(@@root, value)))
        raise "The section file #{value} must contain an array of filenames." unless data.is_a? Array

        # get relative paths to each slide in the array
        sections[key] = data.map do |filename|
          Pathname.new("#{path}/#{filename}").cleanpath.to_path
        end
      end
    else
      raise "The `sections` key must be an Array or Hash, not a #{sections.class}."
    end

  rescue => e
    Showoff::Logger.error "There was a problem with the presentation file #{index}"
    Showoff::Logger.error e.message
    Showoff::Logger.debug e.backtrace
    sections = {}
  end

  sections
end

.get(*setting) ⇒ Object

Retrieve settings from the config hash. If multiple arguments are given then it will dig down through data structures argument by argument.

Returns the data type & value requested, nil on error.



14
15
16
# File 'lib/showoff/config.rb', line 14

def self.get(*setting)
  @@config.dig(*setting) rescue nil
end

.includeNotes?(section) ⇒ Boolean

Identifies whether we’re including a given notes section

Parameters:

  • section (String)

    The name of the notes section of interest.

Returns:

  • (Boolean)

    Whether to include this section in the output



36
37
38
# File 'lib/showoff/config.rb', line 36

def self.includeNotes?(section)
  return true # todo make this work
end

.keysObject



5
6
7
# File 'lib/showoff/config.rb', line 5

def self.keys
  @@config.keys
end

.legacy_sections(data) ⇒ Object

Source:

https://github.com/puppetlabs/showoff/blob/3f43754c84f97be4284bb34f9bc7c42175d45226/lib/showoff_utils.rb#L477-L545


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/showoff/config.rb', line 103

def self.legacy_sections(data)
  # each entry in sections can be:
  # - "filename.md"
  # - "directory"
  # - { "section": "filename.md" }
  # - { "section": "directory" }
  # - { "section": [ "array.md, "of.md, "files.md"] }
  # - { "include": "sections.json" }
  sections = {}
  counters = {}
  lastpath = nil

  data.map do |entry|
    next entry if entry.is_a? String
    next nil unless entry.is_a? Hash
    next entry['section'] if entry.include? 'section'

    section = nil
    if entry.include? 'include'
      file = entry['include']
      path = File.dirname(file)
      data = JSON.parse(File.read(File.join(@@root, file)))
      if data.is_a? Array
        if path == '.'
          section = data
        else
          section = data.map do |source|
            "#{path}/#{source}"
          end
        end
      end
    end
    section
  end.flatten.compact.each do |entry|
    # We do this in two passes simply because most of it was already done
    # and I don't want to waste time on legacy functionality.

    # Normalize to a proper path from presentation root
    if File.directory? File.join(@@root, entry)
      sections[entry] = Dir.glob("#{@@root}/#{entry}/**/*.md").map {|e| e.sub(/^#{@@root}\//, '') }
      lastpath = entry
    else
      path = File.dirname(entry)

      # this lastpath business allows us to reference files in a directory that aren't
      # necessarily contiguous.
      if path != lastpath
        counters[path] ||= 0
        counters[path]  += 1
      end

      # now record the last path we've seen
      lastpath = path

      # and if there are more than one disparate occurences of path, add a counter to this string
      path = "#{path} (#{counters[path]})" unless counters[path] == 1

      sections[path] ||= []
      sections[path]  << entry
    end
  end

  sections
end

.load(path = 'showoff.json') ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/showoff/config.rb', line 40

def self.load(path = 'showoff.json')
  raise 'Presentation file does not exist at the specified path' unless File.exist? path

  @@root     = File.dirname(path)
  @@config   = JSON.parse(File.read(path))
  @@sections = self.expand_sections

  self.load_defaults!
end

.load_defaults!Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/showoff/config.rb', line 168

def self.load_defaults!
  # use a symbol which cannot clash with a string key loaded from json
  @@config['markdown'] ||= :default
  renderer = @@config['markdown']
  defaults = case renderer
    when 'rdiscount'
      {
        :autolink          => true,
      }
    when 'maruku'
      {
        :use_tex           => false,
        :png_dir           => 'images',
        :html_png_url      => '/file/images/',
      }
    when 'bluecloth'
      {
        :auto_links        => true,
        :definition_lists  => true,
        :superscript       => true,
        :tables            => true,
      }
    when 'kramdown'
      {}
    else
      {
        :autolink          => true,
        :no_intra_emphasis => true,
        :superscript       => true,
        :tables            => true,
        :underline         => true,
        :escape_html       => false,
      }
    end

  @@config[renderer] ||= {}
  @@config[renderer]   = defaults.merge!(@@config[renderer])

  # run `wkhtmltopdf --extended-help` for a full list of valid options here
  pdf_defaults = {
    :page_size        => 'Letter',
    :orientation      => 'Portrait',
    :print_media_type => true,
    :quiet            => false}
  pdf_options = @@config['pdf_options'] || {}
  pdf_options = Hash[pdf_options.map {|k, v| [k.to_sym, v]}]

  @@config['pdf_options'] = pdf_defaults.merge!(pdf_options)
end

.path(path) ⇒ Object

Relative path to an item in the presentation directory structure



28
29
30
# File 'lib/showoff/config.rb', line 28

def self.path(path)
  File.expand_path(File.join(@@root, path)).sub(/^#{@@root}\//, '')
end

.rootObject

Absolute root of presentation



23
24
25
# File 'lib/showoff/config.rb', line 23

def self.root
  @@root
end

.sectionsObject



18
19
20
# File 'lib/showoff/config.rb', line 18

def self.sections
  @@sections
end