Module: Glyph::Utils

Included in:
Glyph, Analyzer, Config, Macro, MacroError, Reporter
Defined in:
lib/glyph/utils.rb

Overview

Since:

  • 0.4.0

Instance Method Summary collapse

Instance Method Details

#clean_xml_document(doc) ⇒ Object

Re-indents source code and removes unnecessary spacing

Parameters:

  • the (String)

    XML document to clean up

Since:

  • 0.6.0


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.message
    debug e.backtrace.join("\n")
    doc
  end
end

#complex_output?Boolean

Returns true if Glyph requires two or more conversions

Returns:

  • (Boolean)

Since:

  • 0.5.0


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

Parameters:

Since:

  • 0.6.0


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

Parameters:

  • message (String)

    the message to print

Since:

  • 0.4.0


33
34
35
# File 'lib/glyph/utils.rb', line 33

def debug(message)
  puts message if Glyph.debug?
end

#error(message) ⇒ Object

Prints an error

Parameters:

  • message (String)

    the message to print

Since:

  • 0.4.0


27
28
29
# File 'lib/glyph/utils.rb', line 27

def error(message)
  puts "=> error: #{message}" unless Glyph['system.quiet']
end

#file_copy(source, dest, options = {}) ⇒ Object

An alias for FileUtils#cp

Parameters:

  • source (String)

    the source file

  • dest (String)

    the destination file or directory

  • options (Hash) (defaults to: {})

    copy options

Since:

  • 0.4.0


79
80
81
# File 'lib/glyph/utils.rb', line 79

def file_copy(source, dest, options={})
  FileUtils.cp source, dest, options
end

#file_load(file) ⇒ String

Loads the contents of a file

Parameters:

  • file (#to_s)

    the file to load

Returns:

  • (String)

    the contents of the file

Since:

  • 0.4.0


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

Parameters:

  • file (#to_s)

    the file to write

  • contents (String) (defaults to: "")

    the string to write

Returns:

  • (String)

    the string written to the file

Since:

  • 0.4.0


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

Parameters:

  • message (String)

    the message to print

Since:

  • 0.4.0


15
16
17
# File 'lib/glyph/utils.rb', line 15

def info(message)
  puts "-- #{message}" 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

Parameters:

  • dir (Pathname)

    the directory containing the files

  • extension (String)

    the file extension to check

Yields:

  • (file, contents)

    the file (Pathname) and its contents

Since:

  • 0.4.0


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

Parameters:

Returns:

  • (Boolean)

Since:

  • 0.4.0


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

Parameters:

Since:

  • 0.4.0


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

Parameters:

Since:

  • 0.4.0


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

Parameters:

Returns:

  • (Boolean)

Since:

  • 0.4.0


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

Parameters:

  • message (String)

    the message to print

Since:

  • 0.4.0


9
10
11
# File 'lib/glyph/utils.rb', line 9

def msg(message)
  puts message unless Glyph['system.quiet']
end

#multiple_output_files?Boolean

Returns true if multiple output files are being generated

Returns:

  • (Boolean)

Since:

  • 0.4.0


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

Returns:

  • (Boolean)

Since:

  • 0.4.0


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

Since:

  • 0.5.0


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_sectionsObject

Returns a list of macro names corresponding to sections that commonly have a title

Since:

  • 0.4.0


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

Parameters:

  • message (String)

    the message to print

Since:

  • 0.4.0


21
22
23
# File 'lib/glyph/utils.rb', line 21

def warning(message)
  puts "-> warning: #{message}" unless Glyph['system.quiet']
end

#with_files_from(dir) {|src, dest| ... } ⇒ Object

Iterates through the files in a source directory recursively

Parameters:

  • dir (String)

    the directory to operate on (mirrored in the output directory)

Yields:

  • (src, dest)

    the source file and the corresponding output file

Since:

  • 0.4.0


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

Parameters:

  • file (#to_s)

    the file to write to

  • obj (Object)

    the object to serialize

Since:

  • 0.4.0


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

Parameters:

  • file (#to_s)

    the YAML file to load

Returns:

  • (Object)

    the contents of the YAML file, deserialized

Since:

  • 0.4.0


47
48
49
# File 'lib/glyph/utils.rb', line 47

def yaml_load(file)
  YAML.load_file(file.to_s)
end