Module: Glyph::Utils
Overview
Instance Method Summary collapse
-
#clean_xml_document(doc) ⇒ Object
Re-indents source code and removes unnecessary spacing.
-
#complex_output? ⇒ Boolean
Returns true if Glyph requires two or more conversions.
-
#current_output_setting(setting) ⇒ Object
Returns the value of a setting referred to the current output format.
-
#debug(message) ⇒ Object
Prints a message if running in debug mode.
-
#error(message) ⇒ Object
Prints an error.
-
#file_copy(source, dest, options = {}) ⇒ Object
An alias for FileUtils#cp.
-
#file_load(file) ⇒ String
Loads the contents of a file.
-
#file_write(file, contents = "") ⇒ String
Writes a string to a file.
-
#info(message) ⇒ Object
Prints an informational message.
-
#load_files_from_dir(dir, extension) {|file, contents| ... } ⇒ Object
Loads all child elements of the given directory, matching a given extension.
-
#macro_alias?(name) ⇒ Boolean
Returns true if the macro name is used as an alias.
-
#macro_aliases_for(name) ⇒ Object
Returns the names of the macro aliases referencing the supplied definition.
-
#macro_definition_for(name) ⇒ Object
Returns the name of the macro definition referenced by the supplied alias.
-
#macro_eq?(ident1, ident2) ⇒ Boolean
Returns true if the macro names point to the same definition.
-
#msg(message) ⇒ Object
Prints a message.
-
#multiple_output_files? ⇒ Boolean
Returns true if multiple output files are being generated.
-
#project? ⇒ Boolean
Returns true if the PROJECT constant is set to a valid Glyph project directory.
-
#run_external_command(cmd) ⇒ Object
Execute an external command.
-
#titled_sections ⇒ Object
Returns a list of macro names corresponding to sections that commonly have a title.
-
#warning(message) ⇒ Object
Prints a warning.
-
#with_files_from(dir) {|src, dest| ... } ⇒ Object
Iterates through the files in a source directory recursively.
-
#yaml_dump(file, obj) ⇒ Object
Dumps and serialize an object to a YAML file.
-
#yaml_load(file) ⇒ Object
Loads and deserialiaze the contents of a YAML file.
Instance Method Details
#clean_xml_document(doc) ⇒ Object
Re-indents source code and removes unnecessary spacing
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/glyph/utils.rb', line 186 def clean_xml_document(doc) return doc unless current_output_setting :clean_source begin require 'nokogiri' rescue Exception warning "Cannot clean source because Nokogiri is not installed. Please run: gem install nokogiri" return doc end begin Nokogiri.XML(doc.to_s, &:noblanks).to_xml :indent => 2 rescue Exception => e warning "Unable to clean up source" debug e. debug e.backtrace.join("\n") doc end end |
#complex_output? ⇒ Boolean
Returns true if Glyph requires two or more conversions
123 124 125 126 |
# File 'lib/glyph/utils.rb', line 123 def complex_output? out = Glyph['document.output'] !Glyph["output.#{out}.generator"].blank? || !Glyph["output.#{out}.through"].blank? end |
#current_output_setting(setting) ⇒ Object
Returns the value of a setting referred to the current output format
117 118 119 |
# File 'lib/glyph/utils.rb', line 117 def current_output_setting(setting) Glyph["output.#{Glyph['document.output']}.#{setting}"] end |
#debug(message) ⇒ Object
Prints a message if running in debug mode
33 34 35 |
# File 'lib/glyph/utils.rb', line 33 def debug() puts if Glyph.debug? end |
#error(message) ⇒ Object
Prints an error
27 28 29 |
# File 'lib/glyph/utils.rb', line 27 def error() puts "=> error: #{}" unless Glyph['system.quiet'] end |
#file_copy(source, dest, options = {}) ⇒ Object
An alias for FileUtils#cp
79 80 81 |
# File 'lib/glyph/utils.rb', line 79 def file_copy(source, dest, ={}) FileUtils.cp source, dest, end |
#file_load(file) ⇒ String
Loads the contents of a file
54 55 56 57 58 59 60 61 62 |
# File 'lib/glyph/utils.rb', line 54 def file_load(file) result = "" File.open(file.to_s, 'r:utf-8') do |f| while l = f.gets result << l end end result end |
#file_write(file, contents = "") ⇒ String
Writes a string to a file
68 69 70 71 72 73 |
# File 'lib/glyph/utils.rb', line 68 def file_write(file, contents="") File.open(file.to_s, 'w+:utf-8') do |f| f.print contents end contents end |
#info(message) ⇒ Object
Prints an informational message
15 16 17 |
# File 'lib/glyph/utils.rb', line 15 def info() puts "-- #{}" unless Glyph['system.quiet'] end |
#load_files_from_dir(dir, extension) {|file, contents| ... } ⇒ Object
Loads all child elements of the given directory, matching a given extension
88 89 90 91 92 93 94 |
# File 'lib/glyph/utils.rb', line 88 def load_files_from_dir(dir, extension, &block) if dir.exist? then dir.children.each do |c| block.call(c, file_load(c)) unless c.directory? || c.extname != extension end end end |
#macro_alias?(name) ⇒ Boolean
Returns true if the macro name is used as an alias
130 131 132 |
# File 'lib/glyph/utils.rb', line 130 def macro_alias?(name) ALIASES[:by_alias].include? name.to_sym end |
#macro_aliases_for(name) ⇒ Object
Returns the names of the macro aliases referencing the supplied definition
142 143 144 |
# File 'lib/glyph/utils.rb', line 142 def macro_aliases_for(name) ALIASES[:by_def][name.to_sym] end |
#macro_definition_for(name) ⇒ Object
Returns the name of the macro definition referenced by the supplied alias
136 137 138 |
# File 'lib/glyph/utils.rb', line 136 def macro_definition_for(name) ALIASES[:by_alias][name.to_sym] end |
#macro_eq?(ident1, ident2) ⇒ Boolean
Returns true if the macro names point to the same definition
156 157 158 |
# File 'lib/glyph/utils.rb', line 156 def macro_eq?(ident1, ident2) Glyph::MACROS[ident1.to_sym] == Glyph::MACROS[ident2.to_sym] end |
#msg(message) ⇒ Object
Prints a message
9 10 11 |
# File 'lib/glyph/utils.rb', line 9 def msg() puts unless Glyph['system.quiet'] end |
#multiple_output_files? ⇒ Boolean
Returns true if multiple output files are being generated
168 169 170 |
# File 'lib/glyph/utils.rb', line 168 def multiple_output_files? Glyph["output.#{Glyph['document.output']}.multifile"] end |
#project? ⇒ Boolean
Returns true if the PROJECT constant is set to a valid Glyph project directory
161 162 163 164 165 |
# File 'lib/glyph/utils.rb', line 161 def project? children = ["text", "config.yml", "document.glyph"].sort actual_children = PROJECT.children.map{|c| c.basename.to_s}.sort (actual_children & children) == children end |
#run_external_command(cmd) ⇒ Object
Execute an external command
174 175 176 177 178 179 180 181 |
# File 'lib/glyph/utils.rb', line 174 def run_external_command(cmd) IO.popen(cmd+" 2>&1") do |pipe| pipe.sync = true while str = pipe.gets do puts str end end end |
#titled_sections ⇒ Object
Returns a list of macro names corresponding to sections that commonly have a title
148 149 150 151 |
# File 'lib/glyph/utils.rb', line 148 def titled_sections (Glyph.macro_aliases_for(:section)+ [:section]-[:frontmatter, :bodymatter, :backmatter]).uniq end |
#warning(message) ⇒ Object
Prints a warning
21 22 23 |
# File 'lib/glyph/utils.rb', line 21 def warning() puts "-> warning: #{}" unless Glyph['system.quiet'] end |
#with_files_from(dir) {|src, dest| ... } ⇒ Object
Iterates through the files in a source directory recursively
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/glyph/utils.rb', line 100 def with_files_from(dir, &block) output = complex_output? ? 'tmp' : Glyph['document.output'] dir_path = Glyph::PROJECT/"output/#{output}/#{dir}" dir_path.mkpath (Glyph::PROJECT/dir).find do |i| if i.file? then dest = "#{Glyph::PROJECT/"output/#{output}/#{dir}"}/#{i.relative_path_from(Glyph::PROJECT/dir)}" src = i.to_s Pathname.new(dest).parent.mkpath block.call src, dest end end end |
#yaml_dump(file, obj) ⇒ Object
Dumps and serialize an object to a YAML file
40 41 42 |
# File 'lib/glyph/utils.rb', line 40 def yaml_dump(file, obj) File.open(file.to_s, 'w+') {|f| f.write obj.to_yaml} end |
#yaml_load(file) ⇒ Object
Loads and deserialiaze the contents of a YAML file
47 48 49 |
# File 'lib/glyph/utils.rb', line 47 def yaml_load(file) YAML.load_file(file.to_s) end |