Class: Hyla::Configuration

Inherits:
Hash
  • Object
show all
Defined in:
lib/hyla/configuration.rb

Constant Summary collapse

DEFAULTS =
{
    'source' => Dir.pwd,
    'destination' => File.join(Dir.pwd, 'generated_content'),
    'watch_dir' => '.',
    'watch_ext' => %w(ad adoc asc asciidoc txt index),

    # Asciidoctor
    'backend' => 'html5',
    'doctype' => 'article',
    'compact' => false,
    'to_dir' => '.',
    'to_file' => '',
    'attributes' => {
        'source-highlighter' => 'coderay',
        'linkcss!' => 'true',
        'data-uri' => 'true',
        'stylesheet' => 'asciidoctor.css',
        'stylesdir' => 'styles'
    },
    'safe' => 'unsafe',
    'header_footer' => true
}
IncludeDirectiveRx =

Matches an include preprocessor directive.

Examples

include::chapter1.ad[]
include::example.txt[lines=1;2;5..10]
/^\\?include::([^\[]+)\[(.*?)\]$/
INCLUDE_PREFIX =
'include::'
INCLUDE_SUFFIX =
'[]'
INDEX_SUFFIX =
'_AllSlides.txt'
INDEX_FILE =
'AllSlides.txt'
SNIPPET_TAG =
'snippet'
HEADER_TXT =
"\n"
LEVEL_1 =
'= '
LEVEL_2 =
'== '
SKIP_CHARACTERS =
'>>'
ADOC_EXT =

ADOC_EXT = ‘.ad’

'.adoc'
PREFIX_ARTEFACT =
'asciidoc_'
YAML_CONFIG_FILE_NAME =
'_config.yaml'
ASSETS =
'../../lib/resources/assets'
FONTS =
'../../lib/resources/assets/fonts'
STYLES =
'../../lib/resources/assets/styles'
COVERS =
'../../lib/resources/assets/cover'
TEMPLATES =
'../../lib/templates'
CONFIGS =
'../../lib/configs'
RESOURCES =
'../../lib/resources'
SAMPLES =
'../../lib/templates/sample'
BACKENDS =
'../../lib/resources/backends'
COVER_TEMPLATE =
'../../lib/resources/assets/cover/cover.slim'
NEW_COVER_TEMPLATE =
'../../lib/resources/assets/cover/new_cover.slim'
ASSESSMENT_TXT =
File.open(self.templates + '/course/assessment.txt', 'r').read
LABS_TXT =
File.open(self.templates + '/course/labinstructions.txt', 'r').read
INDEX =
File.open(self.templates + '/course/index.txt', 'r').read
HEADER_INDEX =
File.open(self.templates + '/course/header_index.txt', 'r').read
File.open(self.templates + '/course/footer.txt', 'r').read
COVER_TXT =
File.open(self.templates + '/course/cover.txt', 'r').read
OBJECTIVES_TXT =
File.open(self.templates + '/course/objectives.txt', 'r').read
SUMMARY_TXT =
File.open(self.templates + '/course/summary.txt', 'r').read
AUDIO_TXT =
File.open(self.templates + '/course/audio.txt', 'r').read

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#deep_merge

Instance Attribute Details

#ADOC_EXTObject (readonly)

Returns the value of attribute ADOC_EXT.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def ADOC_EXT
  @ADOC_EXT
end

#assetsObject (readonly)

Returns the value of attribute assets.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def assets
  @assets
end

#backendsObject (readonly)

Returns the value of attribute backends.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def backends
  @backends
end

#configsObject (readonly)

Returns the value of attribute configs.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def configs
  @configs
end

#cover_templateObject (readonly)

Returns the value of attribute cover_template.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def cover_template
  @cover_template
end

#DEFAULTSObject (readonly)

Returns the value of attribute DEFAULTS.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def DEFAULTS
  @DEFAULTS
end

#fontsObject (readonly)

Returns the value of attribute fonts.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def fonts
  @fonts
end

#HEADERObject (readonly)

Returns the value of attribute HEADER.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def HEADER
  @HEADER
end

#HEADER_INDEXObject (readonly)

Returns the value of attribute HEADER_INDEX.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def HEADER_INDEX
  @HEADER_INDEX
end

#INCLUDE_PREFIXObject (readonly)

Returns the value of attribute INCLUDE_PREFIX.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def INCLUDE_PREFIX
  @INCLUDE_PREFIX
end

#INCLUDE_SUFFIXObject (readonly)

Returns the value of attribute INCLUDE_SUFFIX.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def INCLUDE_SUFFIX
  @INCLUDE_SUFFIX
end

#INDEX_SUFFIXObject (readonly)

Returns the value of attribute INDEX_SUFFIX.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def INDEX_SUFFIX
  @INDEX_SUFFIX
end

#LEVEL_1Object (readonly)

Returns the value of attribute LEVEL_1.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def LEVEL_1
  @LEVEL_1
end

#LEVEL_2Object (readonly)

Returns the value of attribute LEVEL_2.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def LEVEL_2
  @LEVEL_2
end

#PREFIX_ARTEFACTObject (readonly)

Returns the value of attribute PREFIX_ARTEFACT.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def PREFIX_ARTEFACT
  @PREFIX_ARTEFACT
end

#resourcesObject (readonly)

Returns the value of attribute resources.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def resources
  @resources
end

#samplesObject (readonly)

Returns the value of attribute samples.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def samples
  @samples
end

#SKIP_CHARACTERSObject (readonly)

Returns the value of attribute SKIP_CHARACTERS.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def SKIP_CHARACTERS
  @SKIP_CHARACTERS
end

#stylesObject (readonly)

Returns the value of attribute styles.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def styles
  @styles
end

#templatesObject (readonly)

Returns the value of attribute templates.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def templates
  @templates
end

#YAML_CONFIG_FILE_NAMEObject (readonly)

Returns the value of attribute YAML_CONFIG_FILE_NAME.



4
5
6
# File 'lib/hyla/configuration.rb', line 4

def YAML_CONFIG_FILE_NAME
  @YAML_CONFIG_FILE_NAME
end

Class Method Details

.assetsObject

Assets Location



118
119
120
# File 'lib/hyla/configuration.rb', line 118

def self.assets
  File.expand_path(ASSETS, File.dirname(__FILE__))
end

.backendsObject

Backends Location



125
126
127
# File 'lib/hyla/configuration.rb', line 125

def self.backends
  File.expand_path(BACKENDS, File.dirname(__FILE__))
end

.configsObject

Config Location



83
84
85
# File 'lib/hyla/configuration.rb', line 83

def self.configs
  File.expand_path(CONFIGS, File.dirname(__FILE__))
end

.cover_templateObject

Cover Slim Template



69
70
71
# File 'lib/hyla/configuration.rb', line 69

def self.cover_template
  File.expand_path(COVER_TEMPLATE, File.dirname(__FILE__))
end

.coversObject

Cover Slim Template



76
77
78
# File 'lib/hyla/configuration.rb', line 76

def self.covers
  File.expand_path(COVERS, File.dirname(__FILE__))
end

.extract_attributes(attributes) ⇒ Object

Retrieve asciidoctor attributes Could be an Arrays of Strings key=value,key=value or Could be a Hash (DEFAULTS, CONFIG_File)



213
214
215
216
217
218
219
220
221
# File 'lib/hyla/configuration.rb', line 213

def self.extract_attributes(attributes)
  result = attributes.split(',')
  attributes = Hash.new
  result.each do |entry|
    words = entry.split('=')
    attributes[words[0]] = words[1]
  end
  return attributes
end

.fontsObject

Fonts Location



111
112
113
# File 'lib/hyla/configuration.rb', line 111

def self.fonts
  File.expand_path(FONTS, File.dirname(__FILE__))
end

.parse(override) ⇒ Object

Public: Generate a Hyla configuration Hash by merging the default options with anything in _config.yaml, and adding the given options on top.

override - A Hash of options that override any options in both

the defaults and the config file. See Hyla::Configuration::DEFAULTS for a
list of option names and their defaults.

Returns the final configuration Hash.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/hyla/configuration.rb', line 144

def self.parse(override)

  # Extract Asciidoctor attributes received from hyla command line '-a key=value,key=value'
  # Convert them to a Hash of attributes 'attributes' => { 'backend' => html5 ... }
  # Assign hash to override[:attributes]
  extracted_attributes = self.extract_attributes(override[:attributes]) if override[:attributes]
  override[:attributes] = extracted_attributes if extracted_attributes

  extracted_email_attributes = self.extract_attributes(override[:email_attributes]) if override[:email_attributes]
  override[:email_attributes] = extracted_email_attributes if extracted_email_attributes

  # Stringify keys of the hash
  override = Configuration[override].stringify_keys
  Hyla::logger2.debug("OVERRIDE Keys: #{override.inspect}")
  
  # Config 
  config = DEFAULTS
  Hyla::logger2.debug("DEFAULTS Keys: #{config.inspect}")

  #
  # Check if config parameter was passed and split content which is a list of files
  # Read each config file and merge content with DEFAULTS
  # Otherwise, check if there is a _config.yaml
  #
  configs = override['config'].split(",").map(&:strip) if override['config']
  if !configs.nil? && !configs.empty?
    configs.each do |cfg_file|
      cfg = read_config_file(cfg_file)
      config = config.deep_merge(cfg)
    end
  else
    new_config = read_config_file(YAML_CONFIG_FILE_NAME)
    Hyla::logger2.debug("OVERRIDE Keys: #{new_config.inspect}") if !new_config.nil?
    config = config.deep_merge(new_config) if !new_config.nil? && !new_config.empty?
  end

  # Merge files with parameters  coming from the cmd line
  config = config.deep_merge(override)
  Hyla::logger2.debug("DEFAULTS Keys: #{config.inspect}")
  # Convert String Keys to Symbols Keys
  config = Configuration[].transform_keys_to_symbols(config)
  Hyla::logger2.debug("Merged Keys: #{config.inspect}")
  return config
end

.read_config_file(filename) ⇒ Object

Read YAML Config file



192
193
194
195
196
197
198
199
# File 'lib/hyla/configuration.rb', line 192

def self.read_config_file(filename)
  f = File.expand_path(filename)
  Hyla::logger2.info("Config file to be parsed : #{f}")
  safe_load_file(f)
rescue SystemCallError
  Hyla::logger2.error "No configuration file retrieved for : #{filename}"
  nil
end

.resourcesObject

Resources Location



97
98
99
# File 'lib/hyla/configuration.rb', line 97

def self.resources
  File.expand_path(RESOURCES, File.dirname(__FILE__))
end

.safe_load_file(filename) ⇒ Object

Load Safely YAML File



204
205
206
# File 'lib/hyla/configuration.rb', line 204

def self.safe_load_file(filename)
  YAML.safe_load_file(filename)
end

.samplesObject

Samples Location



132
133
134
# File 'lib/hyla/configuration.rb', line 132

def self.samples
  File.expand_path(SAMPLES, File.dirname(__FILE__))
end

.stylesObject

Stylesheets Location



104
105
106
# File 'lib/hyla/configuration.rb', line 104

def self.styles
  File.expand_path(STYLES, File.dirname(__FILE__))
end

.templatesObject

Templates Location



90
91
92
# File 'lib/hyla/configuration.rb', line 90

def self.templates
  File.expand_path(TEMPLATES, File.dirname(__FILE__))
end

Instance Method Details

#stringify_keysObject

Public: Turn all keys into string

Return a copy of the hash where all its keys are strings



226
227
228
# File 'lib/hyla/configuration.rb', line 226

def stringify_keys
  reduce({}) { |hsh, (k, v)| hsh.merge(k.to_s => v) }
end

#transform_keys_to_symbols(hash) ⇒ Object

Take keys of hash and transform those to a symbols



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/hyla/configuration.rb', line 233

def transform_keys_to_symbols(hash)
  return hash if not hash.is_a?(Hash)
  hash.inject({}) { |result, (key, value)|
    new_key = case key
                when String then
                  key.to_sym
                else
                  key
              end
    new_value = case value
                  when Hash
                    if key.eql? 'attributes'
                      value
                    else
                      transform_keys_to_symbols(value)
                    end
                  else
                    value
                end
    result[new_key] = new_value
    result
  }
end