Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Snippets are now document-specific.

* Closes #197.
  • Loading branch information...
commit 39aaba947047cefada17bc8b6a44e03576f25d5f 1 parent 819c303
@h3rald authored
View
3  benchmark.rb
@@ -45,7 +45,6 @@ def reset_glyph
reset_glyph
Glyph.run! 'load:all'
-Glyph::SNIPPETS[:test] = text
Benchmark.bm(30) do |x|
sep
@@ -56,7 +55,7 @@ def reset_glyph
puts " => Macro Set: Glyph"
sep
rep(x, "section[...]") { macro_exec "section[#{text}]" }
- rep(x, "snippet[...]") { macro_exec "snippet[test]" }
+ rep(x, "snippet[...]") { macro_exec "snippet:[test|#{text}]snippet[test]" }
rep(x, "textile[...]") { macro_exec "textile[#{text}]" }
rep(x, "markdown[...]") { macro_exec "markdown[#{text}]" }
rep(x, "HTML text") { macro_exec html }
View
5 lib/glyph.rb
@@ -67,9 +67,6 @@ class MutualInclusionError < MacroError; end
# The current version of Glyph
VERSION = file_load(HOME/'VERSION').strip
- # All the currently-loaded snippets
- SNIPPETS = {}
-
# All the currently-loaded macros
MACROS = {}
@@ -171,9 +168,7 @@ def self.reset
self.enable_all
self.config_reset
MACROS.clear
- LAMBDAS.clear
REPS.clear
- SNIPPETS.clear
end
# Reenables all Glyph Rake tasks
View
5 lib/glyph/analyzer.rb
@@ -188,7 +188,8 @@ def stats_link(name)
def stats_snippets
c = @stats[:snippets] = {}
snippets = {}
- c[:definitions] = Glyph::SNIPPETS.keys.sort
+ c[:definitions] = @doc.snippets.keys.sort
+ c[:values] = @doc.snippets
c[:used] = []
c[:unused] = []
c[:total] = 0
@@ -206,7 +207,7 @@ def stats_snippets
def stats_snippet(name)
name = name.to_sym
snippets = {}
- raise ArgumentError, "Snippet '#{name}' does not exist" unless Glyph::SNIPPETS[name]
+ raise ArgumentError, "Snippet '#{name}' does not exist" unless @doc.snippet? name
with_macros(:snippet) do |n|
code = n.parameters[0].to_s.to_sym
if code == name then
View
1  lib/glyph/commands/stats.rb
@@ -34,6 +34,7 @@
no_flags = true
[:macro, :bookmark, :link, :snippet].each do |f|
if options[f] then
+ analyzer.stats_for :snippets if f == :snippet
analyzer.stats_for f, options[f]
no_flags = false
end
View
22 lib/glyph/document.rb
@@ -11,7 +11,8 @@ class Document
ESCAPES = /\\([\\\]\[\|\/=])/
- attr_reader :bookmarks, :placeholders, :headers, :styles, :context, :errors, :todos, :topics, :links, :toc, :fragments
+ attr_reader :bookmarks, :placeholders, :headers, :styles, :context
+ attr_reader :errors, :todos, :topics, :links, :toc, :fragments, :snippets
# Creates a new document
# @param [GlyphSyntaxNode] tree the syntax tree to be evaluate
@@ -24,6 +25,7 @@ def initialize(tree, context={})
@placeholders = {}
@bookmarks = {}
@headers = {}
+ @snippets = {}
@fragments = {}
@styles = []
@errors = []
@@ -50,6 +52,7 @@ def structure
def inherit_from(document, data={})
@bookmarks = document.bookmarks unless data[:bookmarks] == false
@headers = document.headers unless data[:headers] == false
+ @snippets = document.snippets unless data[:snippets] == false
@todos = document.todos unless data[:todos] == false
@styles = document.styles unless data[:styles] == false
@topics = document.topics unless data[:topics] == false
@@ -106,6 +109,23 @@ def header?(key)
@headers[key.to_sym]
end
+ # Stores a new snippet
+ # @param [Symbol, String] key the snippet identifier
+ # @param [string] value the snippet contents
+ # @return [String] the stored snippet
+ # @since 0.5.0
+ def snippet(key, value)
+ @snippets[key.to_sym] = value
+ end
+
+ # Returns a stored snippet or nil
+ # @param [String, Symbol] key the snippet identifier
+ # @return [String, nil] the snippet contents or nil if no snippet is found
+ # @since 0.5.0
+ def snippet?(key)
+ @snippets[key.to_sym]
+ end
+
# @since 0.4.0
# Stores a stylesheet
# @param [String] file the stylesheet file
View
16 lib/glyph/macro.rb
@@ -34,7 +34,7 @@ def update_source(name, file=nil, topic=nil)
file ||= @node[:source][:file] rescue nil
@updated_source = {:name => name, :file => file, :topic => topic}
end
-
+
# Returns a Glyph code representation of the specified parameter
# @param [Fixnum] n the index of the parameter
# @return [String, nil] the string representation of the parameter
@@ -163,7 +163,7 @@ def path
end
macros.reverse.compact.join('/')
end
-
+
# Returns a todo message to include in the document in case of errors.
# @param [String] message the message to include in the document
# @return [String] the resulting todo message
@@ -212,7 +212,6 @@ def interpret(string)
@node[:escape] ? string : inject(string).document.output
end
- # @see Glyph::Document#placeholder
def placeholder(&block)
@node[:document].placeholder &block
end
@@ -237,6 +236,16 @@ def header(hash)
@node[:document].header hash
end
+ # @see Glyph::Document#snippet
+ def snippet(key, value)
+ @node[:document].snippet key, value
+ end
+
+ # @see Glyph::Document#snippet?
+ def snippet?(ident)
+ @node[:document].snippet? ident
+ end
+
# @since 0.5.0
# Renders a macro representation
# @param [Symbol, String] rep the representation to render
@@ -321,6 +330,5 @@ def create_context
context[:document] = @node[:document]
context
end
-
end
end
View
2  lib/glyph/reporter.rb
@@ -94,7 +94,7 @@ def display_snippet
section "Snippet '#{s[:param]}'"
info "Definition:"
puts "-------------------"
- puts SNIPPETS[s[:param]]
+ puts @stats[:snippets][:values][s[:param]]
puts "-------------------"
total :used_instances, s[:stats][:total]
occurrences s[:stats][:files], "Usage Details:" if @detailed
View
2  lib/glyph/utils.rb
@@ -154,7 +154,7 @@ def macro_eq?(ident1, ident2)
# Returns true if the PROJECT constant is set to a valid Glyph project directory
def project?
- children = ["text", "snippets.yml", "config.yml", "document.glyph"].sort
+ children = ["text", "config.yml", "document.glyph"].sort
actual_children = PROJECT.children.map{|c| c.basename.to_s}.sort
(actual_children & children) == children
end
View
7 macros/core.rb
@@ -2,12 +2,11 @@
# encoding: utf-8
macro :snippet do
- no_mutual_inclusion_in 0
ident = value.to_sym
- if Glyph::SNIPPETS.has_key? ident then
+ if snippet? ident then
begin
update_source "snippet[#{ident}]"
- interpret Glyph::SNIPPETS[ident]
+ interpret snippet?(ident)
rescue Exception => e
case
when e.is_a?(Glyph::MutualInclusionError) then
@@ -28,7 +27,7 @@
exact_parameters 2
ident = param(0)
text = param(1)
- Glyph::SNIPPETS[ident.to_sym] = text
+ snippet ident, text
""
end
View
2  spec/lib/commands_spec.rb
@@ -234,10 +234,8 @@
out = run_command(["stats", "-ms"])
total_macros = (Glyph::MACROS.keys - Glyph::ALIASES[:by_alias].keys).uniq.length
out.should match "-- Total Macro Definitions: #{total_macros}"
- out.should match "-- Unused Snippets: test"
out = run_command(["stats"])
out.should match "-- Total Macro Definitions: #{total_macros}"
- out.should_not match "-- Unused Snippets: test"
out.should match "-- Total Unreferenced Bookmarks: 3"
out = run_command(["stats", "-lb", "--bookmark=md"])
out.should match "-- Unreferenced Bookmarks: h_1, h_2, md"
View
4 spec/lib/glyph_spec.rb
@@ -56,11 +56,11 @@
(Glyph::PROJECT/'article.html').exist?.should == true
end
- it "should provide a reset method to remove config overrides, reenable tasks, clear macros and snippets." do
+ it "should provide a reset method to remove config overrides, reenable tasks, clear macros and reps" do
Glyph['test_setting'] = true
Glyph.reset
- Glyph::SNIPPETS.length.should == 0
Glyph::MACROS.length.should == 0
+ Glyph::REPS.length.should == 0
Glyph['test_setting'].should == nil
end
View
3  spec/lib/macro_validators_spec.rb
@@ -36,8 +36,7 @@
end
it "should check for mutual inclusion" do
- Glyph::SNIPPETS[:inc] = "Test &[inc]"
- lambda {interpret("&[inc] test").document}.should raise_error(Glyph::MutualInclusionError)
+ interpret("&:[inc|Test &[inc]]&[inc] test").document.output.should == "Test [SNIPPET 'inc' NOT PROCESSED] test"
end
it "should validate XML elements" do
View
1  spec/lib/reporter_spec.rb
@@ -84,6 +84,7 @@ def rep
end
it "should display stats for a single snippet" do
+ stats :snippets
stats :snippet, :test
out = stdout_for { rep.display }
out.should match "-- Total Used Instances: 2"
View
12 spec/macros/core_spec.rb
@@ -15,26 +15,22 @@
it "snippet" do
define_em_macro
- interpret "Testing a snippet: &[test]."
+ interpret "&:[test|This is a \nTest snippet]Testing a snippet: &[test]."
@p.document.output.should == "Testing a snippet: This is a \nTest snippet."
interpret("Testing &[wrong].")
@p.document.output.should == "Testing [SNIPPET 'wrong' NOT PROCESSED]."
- Glyph::SNIPPETS[:a] = "this is a em[test] &[b]"
- Glyph::SNIPPETS[:b] = "and another em[test]"
- text = "TEST: &[a]"
+ text = "&:[b|and another em[test]]&:[a|this is a em[test] &[b]]TEST: &[a]"
interpret text
@p.document.output.should == "TEST: this is a <em>test</em> and another <em>test</em>"
# Check snippets with links
- Glyph::SNIPPETS[:c] = "This is a link to something afterwards: =>[#other]"
- text = "Test. &[c]. #[other|Test]."
+ text = "&:[c|This is a link to something afterwards: =>[#other]]Test. &[c]. #[other|Test]."
output_for(text).should == %{Test. This is a link to something afterwards: <a href="#other">Test</a>. <a id="other">Test</a>.}
end
it "snippet:" do
interpret("&[t1] - &:[t1|Test #1] - &[t1]")
@p.document.output.should == "[SNIPPET 't1' NOT PROCESSED] - - Test #1"
- Glyph::SNIPPETS[:t1].should == "Test #1"
- Glyph::SNIPPETS.delete :t1
+ @p.document.snippet?(:t1).should == "Test #1"
end
it "condition" do
View
2  spec/spec_helper.rb
@@ -62,7 +62,7 @@ def delete_project_dir
def delete_project
delete_project_dir
- Glyph::SNIPPETS.clear
+ Glyph::REPS.clear
Glyph::MACROS.clear
Glyph['document.source'] = 'document.glyph'
Glyph.document = nil
View
12 spec/tasks/load_spec.rb
@@ -16,18 +16,6 @@
delete_project
lambda { Glyph.run! 'load:all' }.should raise_error
end
-
- it "[snippets] should load snippet definitions" do
- lambda { Glyph.run! 'load:snippets'}.should_not raise_error
- Glyph::SNIPPETS[:test].blank?.should == false
- end
-
- it "[snippets] should not load snippets.xml in Lite mode" do
- Glyph.lite_mode = true
- lambda { Glyph.run! 'load:snippets'}.should_not raise_error
- Glyph::SNIPPETS[:test].blank?.should == true
- Glyph.lite_mode = false
- end
it "[macros] should load macro definitions" do
lambda { Glyph.run! 'load:macros'}.should_not raise_error
View
12 tasks/load.rake
@@ -4,7 +4,7 @@ namespace :load do
include Glyph::Utils
desc "Load all files"
- task :all => [:config, :tasks, :commands, :snippets, :macros] do
+ task :all => [:config, :tasks, :commands, :macros] do
end
desc "Load tasks"
@@ -26,16 +26,6 @@ namespace :load do
end
end
- desc "Load snippets"
- task :snippets do
- unless Glyph.lite? then
- raise RuntimeError, "The current directory is not a valid Glyph project" unless Glyph.project?
- snippets = yaml_load Glyph::PROJECT/'snippets.yml'
- raise RuntimeError, "Invalid snippets file" unless snippets.blank? || snippets.is_a?(Hash)
- Glyph::SNIPPETS.replace snippets
- end
- end
-
desc "Load macros"
task :macros do
raise RuntimeError, "The current directory is not a valid Glyph project" unless Glyph.project? || Glyph.lite?
View
2  tasks/project.rake
@@ -11,8 +11,6 @@ namespace :project do
# Create subdirectories
subdirs = ['lib/macros', 'lib/tasks', 'lib/layouts', 'lib/tasks', 'lib/commands', 'text', 'output', 'images', 'styles']
subdirs.each {|d| (dir/d).mkpath }
- # Create snippets
- yaml_dump Glyph::PROJECT/'snippets.yml', {:test => "This is a \nTest snippet"}
# Create files
file_copy Glyph::HOME/'document.glyph', Glyph::PROJECT/'document.glyph'
config = yaml_load Glyph::HOME/'config.yml'
Please sign in to comment.
Something went wrong with that request. Please try again.