Module: Glyph

Extended by:
Utils
Defined in:
lib/glyph.rb,
lib/glyph/macro.rb,
lib/glyph/utils.rb,
lib/glyph/config.rb,
lib/glyph/parser.rb,
lib/glyph/analyzer.rb,
lib/glyph/bookmark.rb,
lib/glyph/document.rb,
lib/glyph/reporter.rb,
lib/glyph/interpreter.rb,
lib/glyph/syntax_node.rb,
lib/glyph/macro_validators.rb

Overview

Glyph is a Rapid Document Authoring Framework able to produce structured documents effortlessly.

Defined Under Namespace

Modules: Utils Classes: Analyzer, AttributeNode, Bookmark, Config, Document, DocumentNode, Error, EscapeNode, Header, Interpreter, Macro, MacroError, MacroNode, MutualInclusionError, ParameterNode, Parser, Reporter, SyntaxError, SyntaxNode, TextNode

Constant Summary collapse

LIB =

The directory containing Glyph library files

Pathname(__FILE__).dirname.expand_path/'glyph'
HOME =

The directory containing the full Glyph installation

LIB/'../../'
SPEC_DIR =

The directory containing all Glyph tests

Pathname(__FILE__).dirname.expand_path/'../spec'
TASKS_DIR =

The directory containing all Glyph Rake tasks

Pathname(__FILE__).dirname.expand_path/'../tasks'
VERSION =

The current version of Glyph

"0.5.3.1"
MACROS =

All the currently-loaded macros

{}
REPS =

All the currently-loaded macro representations

{}
ALIASES =

All macro aliases

{:by_alias => {}, :by_def => {}}
PROJECT =

The directory of the current Glyph project.

(Glyph.test?) ? Glyph::SPEC_DIR/"test_project" : Pathname.new(Dir.pwd)
CONFIG =

Glyph’s configuration

Glyph::Config.new :resettable => true, :mutable => false
SYSTEM_CONFIG =

System configuration

Glyph::Config.new(:file => HOME/'config.yml')
GLOBAL_CONFIG =

Global configuration

Glyph.test? ? Glyph::Config.new(:file => SPEC_DIR/'.glyphrc') : Glyph::Config.new(:file => home_dir/'.glyphrc')
PROJECT_CONFIG =

Project configuration

Glyph::Config.new(:file => PROJECT/'config.yml', :resettable => true) rescue Glyph::Config.new(:resettable => true, :mutable => true)
@@document =

The main document being generated by Glyph

nil

Class Method Summary collapse

Methods included from Utils

clean_xml_document, complex_output?, current_output_setting, debug, error, file_copy, file_load, file_write, info, load_files_from_dir, macro_alias?, macro_aliases_for, macro_definition_for, macro_eq?, msg, multiple_output_files?, project?, run_external_command, titled_sections, warning, with_files_from, yaml_dump, yaml_load

Class Method Details

.[](setting) ⇒ Object

Returns the value of a configuration setting



148
149
150
# File 'lib/glyph.rb', line 148

def self.[](setting)
	Glyph::CONFIG.get(setting)
end

.[]=(setting, value) ⇒ Object

Overrides a configuration setting

Parameters:

  • setting (String, Symbol)

    the configuration setting to change

  • value

    the new value



155
156
157
158
# File 'lib/glyph.rb', line 155

def self.[]=(setting, value)
	PROJECT_CONFIG.set setting, value
	self.config_refresh
end

.compile(src, out = nil) ⇒ Object

Compiles a single Glyph file

Parameters:

  • src (String)

    the full or relative path to the source file

  • out (String) (defaults to: nil)

    the full or relative path to the output file



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/glyph.rb', line 274

def self.compile(src, out=nil)
	pwd = Dir.pwd
	Dir.chdir Pathname.new(src).parent.to_s
	begin
		require 'glyph/commands'
		self['system.quiet'] = true
		self.library_mode = true
		TOPLEVEL_BINDING.eval('self').run ["compile", src.to_s, out].compact	
	rescue Exception => e
		raise 
	ensure
		Dir.chdir pwd
		self.library_mode = false
		self.lite_mode = false
		self['system.quiet'] = false
	end
end

.config_refreshObject

Restores Glyph configuration (keeping all overrides and project settings)



161
162
163
164
# File 'lib/glyph.rb', line 161

def self.config_refresh
	CONFIG.merge!(SYSTEM_CONFIG.merge(GLOBAL_CONFIG.merge(PROJECT_CONFIG)))
	Glyph.safe_mode = Glyph['options.safe_mode']
end

.config_resetObject

Resets Glyph configuration (removing all overrides and project settings)



167
168
169
170
171
# File 'lib/glyph.rb', line 167

def self.config_reset
	Glyph::CONFIG.reset
	Glyph::PROJECT_CONFIG.reset
	self.config_refresh
end

.define(name, text) ⇒ Object

Defines a new macro in Glyph code.

Parameters:

  • name (Symbol, String)

    the name of the macro

  • text (String)

    the Glyph code used to define the macro

Since:

  • 0.5.0



243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/glyph.rb', line 243

def self.define(name, text)
	macro name do
		body = text.dup
		# Parameters
		body.gsub!(/\{\{(\d+)\}\}/) do
			raw_param($1.to_i).to_s.strip
		end
		# Attributes
		body.gsub!(/\{\{([^\[\]\|\\\s]+)\}\}/) do
			raw_attr($1.to_sym).to_s.strip
		end
		interpret body
	end
end

.documentObject

Used to access @@document



138
139
140
# File 'lib/glyph.rb', line 138

def self.document
	@@document
end

.document=(document) ⇒ Object

Used to set @@document



143
144
145
# File 'lib/glyph.rb', line 143

def self.document=(document)
	@@document = document
end

.enable(task) ⇒ Object

Reenables a Rake task

Parameters:



191
192
193
# File 'lib/glyph.rb', line 191

def self.enable(task)
	Rake::Task[task].reenable
end

.enable_allObject

Reenables all Glyph Rake tasks



185
186
187
# File 'lib/glyph.rb', line 185

def self.enable_all
	Rake::Task.tasks.each {|t| t.reenable }
end

.filter(text) ⇒ String

Converts a text containing Glyph markup language into the current Glyph output target.

Note Only ‘html’ is supported as output target for now.

Examples:

require 'glyph'
Glyph.filter "section[header[Test]\nA Test section...]"

Parameters:

  • text (String)

    the text to convert

Returns:

  • (String)

    the converted text



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/glyph.rb', line 300

def self.filter(text)
	self.lite_mode = true
	self.enable_all
	result = ""
	begin
		self['system.quiet'] = true
		self.library_mode = true
		self.run 'load:all'
		result = Interpreter.new(text).document.output
	rescue Exception => e
		raise 
	ensure
		self.lite_mode = false
		self.library_mode = false
		self['system.quiet'] = false
	end
	result
end

.macro(name, &block) ⇒ Object

Defines a new macro

Parameters:



212
213
214
# File 'lib/glyph.rb', line 212

def self.macro(name, &block)
	MACROS[name.to_sym] = block
end

.macro_alias(pair) ⇒ Object

Defines an alias for an existing macro

Examples:

{:old_name => :new_name}

Parameters:

  • text (Hash)

    the single-key hash defining the alias



262
263
264
265
266
267
268
269
# File 'lib/glyph.rb', line 262

def self.macro_alias(pair)
	name = pair.keys[0].to_sym
	orig = pair.values[0].to_sym
	ALIASES[:by_def][orig] = [] unless ALIASES[:by_def][orig]
	ALIASES[:by_def][orig] << name unless ALIASES[:by_def][orig].include? name
	ALIASES[:by_alias][name] = orig 
	MACROS[name] = MACROS[orig]
end

.project_reps_for(output) ⇒ Object

Loads project macro representations for a given output

Parameters:

Since:

  • 0.5.0



235
236
237
# File 'lib/glyph.rb', line 235

def self.project_reps_for(output)
	Glyph.instance_eval file_load(Glyph::PROJECT/"lib/macros/reps/#{output}.rb") rescue nil
end

.rep(name, &block) ⇒ Object

Defines a new macro representation

Parameters:

Since:

  • 0.5.0



219
220
221
222
223
# File 'lib/glyph.rb', line 219

def self.rep(name, &block)
	REPS[name.to_sym] = block
	# Mirror aliases as well
	ALIASES[:by_def][name.to_sym].to_a.each { |a| REPS[a] = block }
end

.reps_for(output) ⇒ Object

Loads macro representations for a given output

Parameters:

Since:

  • 0.5.0



228
229
230
# File 'lib/glyph.rb', line 228

def self.reps_for(output)
	Glyph.instance_eval file_load(Glyph::HOME/"macros/reps/#{output}.rb") rescue nil
end

.resetObject

Resets Glyph completely, i.e.:

  • Re-enables all Glyph Rake tasks

  • Resets the configuration to system defaults

  • Clears macros and snippets



177
178
179
180
181
182
# File 'lib/glyph.rb', line 177

def self.reset
	self.enable_all
	self.config_reset
	MACROS.clear
	REPS.clear
end

.run(task, *args) ⇒ Object

Runs a Rake task

Parameters:

  • task (Symbol, String)

    the task to run

  • *args

    the task arguments



206
207
208
# File 'lib/glyph.rb', line 206

def self.run(task, *args)
	Rake::Task[task].invoke *args
end

.run!(task, *args) ⇒ Object

Reenables and runs a Rake task

Parameters:

  • task (Symbol, String)

    the task to run

  • *args

    the task arguments



198
199
200
201
# File 'lib/glyph.rb', line 198

def self.run!(task, *args)
	Rake::Task[task].reenable
	self.run task, *args
end

.setupObject

Loads all Rake tasks



131
132
133
134
135
# File 'lib/glyph.rb', line 131

def self.setup
	FileList["#{TASKS_DIR}/**/*.rake"].each do |f|
		load f
	end	
end