Skip to content

Commit

Permalink
Implemented locked system.* configuration namespace.
Browse files Browse the repository at this point in the history
* Closes #111.
  • Loading branch information
h3rald committed May 10, 2010
1 parent 7cd6b9e commit d37142f
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 90 deletions.
1 change: 1 addition & 0 deletions book/text/ref_config.glyph
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
![Remove system settings from documentation]
section[header[@document.*@]
The following configuration settings are related to the current Glyph document. Therefore, you should update them right after creating a project.

Expand Down
1 change: 1 addition & 0 deletions book/text/troubleshooting.glyph
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
![New error: Cannot reset '#@value' setting (system use only).]
This chapter lists the most common error messages that can be returned when running a Glyph command. It does not aim to be an exhaustive list, especially if you =>[#extending|extended] Glyph by creating your own macros or you're embedding Ruby code using the %>[ruby] macro.

tip[As a general rule, more diagnostic information is provided if Glyph is run in =>[#debug_switch|debug mode].]
Expand Down
98 changes: 50 additions & 48 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,56 @@
:draft: false
:output: 'html'
:output_targets: [:html, :pdf]
:structure:
:hidden:
- :imprint
- :dedication
- :inspiration
- :postscript
- :colophon
- :promotion
:special:
- :foreword
- :acknowledgement
- :prologue
- :epilogue
- :addendum
- :glossary
- :bibliography
- :references
- :index
- :lot
- :lof
:frontmatter:
- :preface
- :imprint
- :dedication
- :inspiration
- :foreword
- :introduction
- :acknowledgement
- :prologue
:bodymatter:
- :volume
- :book
- :part
- :chapter
:backmatter:
- :epilogue
- :afterword
- :postscript
- :appendix
- :addendum
- :glossary
- :colophon
- :bibliography
- :promotion
- :references
- :index
- :lot
- :lof
:system:
:quiet: false
:structure:
:hidden:
- :imprint
- :dedication
- :inspiration
- :postscript
- :colophon
- :promotion
:special:
- :foreword
- :acknowledgement
- :prologue
- :epilogue
- :addendum
- :glossary
- :bibliography
- :references
- :index
- :lot
- :lof
:frontmatter:
- :preface
- :imprint
- :dedication
- :inspiration
- :foreword
- :introduction
- :acknowledgement
- :prologue
:bodymatter:
- :volume
- :book
- :part
- :chapter
:backmatter:
- :epilogue
- :afterword
- :postscript
- :appendix
- :addendum
- :glossary
- :colophon
- :bibliography
- :promotion
- :references
- :index
- :lot
- :lof
:tools:
:pdf_generator: 'prince'
:highlighters:
Expand Down
14 changes: 7 additions & 7 deletions lib/glyph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def self.compile(src, out=nil)
Dir.chdir Pathname.new(src).parent.to_s
begin
require 'glyph/commands'
self[:quiet] = true
self['system.quiet'] = true
self.library_mode = true
GLI.run ["compile", src.to_s, out].compact
rescue Exception => e
Expand All @@ -207,7 +207,7 @@ def self.compile(src, out=nil)
Dir.chdir pwd
self.library_mode = false
self.lite_mode = false
self[:quiet] = false
self['system.quiet'] = false
end
end

Expand All @@ -224,7 +224,7 @@ def self.filter(text)
self.enable_all
result = ""
begin
self[:quiet] = true
self['system.quiet'] = true
self.library_mode = true
self.run 'load:all'
result = Interpreter.new(text).document.output
Expand All @@ -233,27 +233,27 @@ def self.filter(text)
ensure
self.lite_mode = false
self.library_mode = false
self[:quiet] = false
self['system.quiet'] = false
end
result
end

# Prints a message
# @param [String] message the message to print
def self.info(message)
puts "#{message}" unless Glyph[:quiet]
puts "#{message}" unless Glyph['system.quiet']
end

# Prints a warning
# @param [String] message the message to print
def self.warning(message)
puts "--> warning: #{message}" unless Glyph[:quiet]
puts "--> warning: #{message}" unless Glyph['system.quiet']
end

# Prints an error
# @param [String] message the message to print
def self.error(message)
puts "==> error: #{message}" unless Glyph[:quiet]
puts "==> error: #{message}" unless Glyph['system.quiet']
end

end
Expand Down
22 changes: 13 additions & 9 deletions lib/glyph/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,19 @@
raise RuntimeError, "Unknown setting '#{args[0]}'" if setting.blank?
Glyph.info setting
when 2 then
# Remove all overrides
Glyph.config_reset
# Reload current project config
config.read
config.set args[0], args[1]
# Write changes to file
config.write
# Refresh configuration
Glyph.config_refresh
if args[0].match /^system\..+/ then
Glyph.warning "Cannot reset '#@value' setting (system use only)."
else
# Remove all overrides
Glyph.config_reset
# Reload current project config
config.read
config.set args[0], args[1]
# Write changes to file
config.write
# Refresh configuration
Glyph.config_refresh
end
else
raise ArgumentError, "Too many arguments."
end
Expand Down
6 changes: 3 additions & 3 deletions lib/glyph/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ def read
# @raise [ArgumentError] if the setting refers to an invalid namespace
# @example
# cfg = Glyph::Config.new
# cfg.set "quiet", true # Sets :quiet => true
# cfg.set "system.quiet", true # Sets "system.quiet" => true
# cfg.set "test.test_value", "[1,2,3]" # Sets :test => {:test_value => [1,2,3]}
# cfg.sef :quiet, "false" # Sets :quiet => false
# cfg.set "system.quiet", "false" # Sets "system.quiet" => false
def set(setting, value)
raise RuntimeError, "Configuration cannot be changed" unless @options[:mutable]
if value.is_a?(String) && value.match(/^(["'].*["']|:.+|\[.*\]|\{.*\}|true|false|nil)$/) then
Expand Down Expand Up @@ -122,7 +122,7 @@ def set(setting, value)
# @see Glyph::Config#set
# @example
# cfg = Glyph::Config.new
# cfg.get :quiet # true
# cfg.get "system.quiet" # true
# cfg.get "test.test_value" # [1,2,3]
def get(setting)
@data.instance_eval "self#{setting.to_s.split(".").map{|key| "[:#{key}]" }.join}" rescue nil
Expand Down
6 changes: 5 additions & 1 deletion macros/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@
end

macro :config do
Glyph[@value]
if @value.match /^system\..+/ then
macro_warning "Cannot reset '#@value' setting (system use only)."
else
Glyph[@value]
end
end

macro "config:" do
Expand Down
8 changes: 4 additions & 4 deletions macros/html/structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
h_id = nil if h_id.blank?
level = 1
@node.ascend do |n|
if Glyph["structure.headers"].include? n[:macro] then
if Glyph["system.structure.headers"].include? n[:macro] then
level+=1
end
end
Expand Down Expand Up @@ -93,8 +93,8 @@
list = ""
added_headers ||= []
n1.descend do |n2, level|
if Glyph['structure.headers'].include?(n2[:macro])
next if n2.find_parent{|node| Glyph['structure.special'].include? node[:macro] }
if Glyph['system.structure.headers'].include?(n2[:macro])
next if n2.find_parent{|node| Glyph['system.structure.special'].include? node[:macro] }
header_id = n2.children.select{|n| n[:header]}[0][:header] rescue nil
next if added_headers.include? header_id
added_headers << header_id
Expand Down Expand Up @@ -126,7 +126,7 @@

macro_alias :section => :div

(Glyph['structure.frontmatter'] + Glyph['structure.bodymatter'] + Glyph['structure.backmatter']).
(Glyph['system.structure.frontmatter'] + Glyph['system.structure.bodymatter'] + Glyph['system.structure.backmatter']).
each {|s| macro_alias s => :div }

macro_alias :frontcover => :div
Expand Down
7 changes: 7 additions & 0 deletions spec/lib/commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
(Glyph::SPEC_DIR/'.glyphrc').unlink
end

it "[config] should not overwrite system settings" do
create_project
Glyph['system.test_setting'] = false
run_command(["config", "system.test_setting", true]).match(/warning.+\(system use only\)/m).should_not == nil
Glyph['system.test_setting'].should == false
end

it "[add] should create a new text file" do
create_project
run_command_successfully(["add", "test.textile"]).should == true
Expand Down
12 changes: 6 additions & 6 deletions spec/lib/glyph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@
arr.each {|v| total += 1; Glyph::MACROS[v.to_sym].should == Glyph::MACROS[target.to_sym]}
end
aliases.each { |v| check_aliases.call v[0], v[1] }
check_aliases.call Glyph['structure.frontmatter'], :div
check_aliases.call Glyph['structure.bodymatter'], :div
check_aliases.call Glyph['structure.backmatter'], :div
Glyph['structure.frontmatter'].length.should == 8
Glyph['structure.bodymatter'].length.should == 4
Glyph['structure.backmatter'].length.should == 13
check_aliases.call Glyph['system.structure.frontmatter'], :div
check_aliases.call Glyph['system.structure.bodymatter'], :div
check_aliases.call Glyph['system.structure.backmatter'], :div
Glyph['system.structure.frontmatter'].length.should == 8
Glyph['system.structure.bodymatter'].length.should == 4
Glyph['system.structure.backmatter'].length.should == 13
#puts Glyph::MACROS.keys.map{|i| i.to_s}.sort.to_yaml
total.should == Glyph::MACROS.length
end
Expand Down
3 changes: 3 additions & 0 deletions spec/macros/macros_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@
@p.document.output.should == %{test.setting = TEST}
interpret "test.setting = $:[test.setting|TEST2]$[test.setting]"
@p.document.output.should == %{test.setting = TEST2}
Glyph['system.test'] = false
interpret "$:[system.test|true]"
# TODO
end

it "toc" do
Expand Down
8 changes: 4 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ module Glyph; end

require "glyph"

Glyph[:quiet] = true
Glyph['system.quiet'] = true

def create_project_dir
Glyph::PROJECT.mkpath
end

def reset_quiet
Glyph.reset
Glyph[:quiet] = true
Glyph['system.quiet'] = true
end

def create_project
Expand Down Expand Up @@ -61,9 +61,9 @@ def run_command(cmd)
old_stderr = $stderr
$stdout = out
$stderr = out
Glyph[:quiet] = false
Glyph['system.quiet'] = false
GLI.run cmd
Glyph[:quiet] = true
Glyph['system.quiet'] = true
$stdout = old_stdout
$stderr = old_stderr
out.string
Expand Down
4 changes: 2 additions & 2 deletions spec/tasks/load_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
it "[config] should load configuration files and apply overrides" do
Glyph.config_refresh
lambda { Glyph.run! 'load:config'}.should_not raise_error
Glyph[:quiet] = true
Glyph['system.quiet'] = true
Glyph::PROJECT_CONFIG.blank?.should == false
Glyph::SYSTEM_CONFIG.blank?.should == false
Glyph['structure.headers'].class.to_s.should == "Array"
Glyph['system.structure.headers'].class.to_s.should == "Array"
end

end
10 changes: 5 additions & 5 deletions tasks/load.rake
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ namespace :load do
Glyph::SYSTEM_CONFIG.read
Glyph::GLOBAL_CONFIG.read
Glyph.config_refresh
Glyph["structure.headers"] = [:section] +
Glyph['structure.frontmatter'] +
Glyph['structure.backmatter'] +
Glyph['structure.bodymatter'] -
Glyph['structure.hidden']
Glyph["system.structure.headers"] = [:section] +
Glyph['system.structure.frontmatter'] +
Glyph['system.structure.backmatter'] +
Glyph['system.structure.bodymatter'] -
Glyph['system.structure.hidden']
end

end
2 changes: 1 addition & 1 deletion tasks/project.rake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace :project do
config[:document][:filename] = dir.basename.to_s
config[:document][:title] = dir.basename.to_s
config[:document][:author] = ENV['USER'] || ENV['USERNAME']
config.delete(:structure)
config.delete(:system)
yaml_dump Glyph::PROJECT/'config.yml', config
Glyph.info "Project '#{dir.basename}' created successfully."
end
Expand Down

0 comments on commit d37142f

Please sign in to comment.