Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented macro definition and loading (untested).

  • Loading branch information...
commit 0ecf6b886cf35edfad9623d9983d36255eb1f7ea 1 parent e3e8180
@h3rald authored
View
7 bin/glyph
@@ -1,13 +1,6 @@
#!/usr/bin/ruby
$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
-require 'rubygems'
-
-require 'gli'
-include GLI
-
-alias gli_desc desc # desc is used by Rake as well...
require 'glyph'
-
require 'glyph/commands'
View
4 lib/glyph/commands.rb
@@ -1,5 +1,9 @@
#!/usr/bin/env ruby
+include GLI
+
+alias gli_desc desc # desc is used by Rake as well...
+
gli_desc 'Create a new Glyph project'
arg_name "project_name"
command :init do |c|
View
10 lib/glyph/macros.rb
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+
+macro :note do |params|
+ %{
+ <div class="note">
+ <span class="note-title">Note</span>
+ <span class="note-body">#{params[0]}</span>
+ </div>
+ }
+end
View
10 lib/glyph/preprocessor.rb
@@ -10,13 +10,17 @@ module Preprocessor
def process(text)
text.gsub(MACRO_REGEX) do |m|
# Note: pipes (|) cannot be escaped; use &#124; instead.
- m.replace macro($1, $2.split('|'))
+ m.replace run($1, $2.split('|'))
end
end
- def macro(macro, *params)
+ def run(macro, *params)
raise RuntimeError, "Undefined macro '#{macro}'" unless Glyph::MACROS.include? macro
- Glyph::MACROS[macro].run
+ Glyph::MACROS[macro].call params
+ end
+
+ def macro(name, &block)
+ Glyph::MACROS[name.to_s] = block
end
end
View
17 lib/glyph/system_extensions.rb
@@ -18,4 +18,21 @@ def yaml_load(file)
YAML.load_file(file.to_s)
end
+ def file_load(file)
+ begin
+ File.open(file, 'r') do |f|
+ while l = f.gets
+ result << l
+ end
+ end
+ result
+ rescue
+ raise RuntimeError, "Unable to read file '#{file}'"
+ end
+ end
+
+ def file_copy(source, dest, options={})
+ FileUtils.cp source, dest, options
+ end
+
end
View
5 spec/tasks/load_spec.rb
@@ -16,4 +16,9 @@
Glyph::SNIPPETS[:test].blank?.should == false
end
+ it "[macros] should load macro definitions" do
+ lambda { Glyph.run! 'load:macros'}.should_not raise_error
+ Glyph::MACROS['note'].blank?.should == false
+ end
+
end
View
2  spec/tasks/project_spec.rb
@@ -19,7 +19,7 @@
it "[create] should create a new project if an existing empty directory is supplied" do
lambda { Glyph.run! 'project:create', Glyph::PROJECT }.should_not raise_error
(Glyph::PROJECT/'lib/tasks').exist?.should == true
- (Glyph::PROJECT/'lib/macros').exist?.should == true
+ (Glyph::PROJECT/'lib/macros.rb').exist?.should == true
(Glyph::PROJECT/'text').exist?.should == true
(Glyph::PROJECT/'layouts').exist?.should == true
(Glyph::PROJECT/'styles').exist?.should == true
View
0  spec/test_project/text/test.textile
No changes.
View
4 tasks/load.rake
@@ -9,5 +9,9 @@ namespace :load do
Glyph::SNIPPETS.replace snippets
end
+ desc "Load macros"
+ task :macros do
+ Glyph::Preprocessor.instance_eval file_load(Glyph::PROJECT/'lib/macros.rb')
+ end
end
View
3  tasks/project.rake
@@ -8,8 +8,9 @@ namespace :project do
raise ArgumentError, "Directory #{dir} does not exist." unless dir.exist?
raise ArgumentError, "Directory #{dir} is not empty." unless dir.children.blank?
# Create subdirectories
- subdirs = ['lib/tasks', 'lib', 'text', 'output', 'lib/macros', 'assets', 'layouts', 'styles']
+ subdirs = ['lib/tasks', 'lib', 'text', 'output', 'assets', 'layouts', 'styles']
subdirs.each {|d| (dir/d).mkpath }
+ file_copy Glyph::LIB_DIR/'macros.rb', Glyph::PROJECT/'lib/macros.rb'
# Create snippets
yaml_dump Glyph::PROJECT/'snippets.yml', {:test => "This is a \nTest snippet"}
# Create files

0 comments on commit 0ecf6b8

Please sign in to comment.
Something went wrong with that request. Please try again.