[DRE-commits] [ruby-org] 69/303: Added support for in-buffer TODO keywords

Jérémy Bobbio lunar at alioth.debian.org
Fri Aug 9 17:33:29 UTC 2013


This is an automated email from the git hooks/post-receive script.

lunar pushed a commit to branch master
in repository ruby-org.

commit 4ddcdfa6e2790447599642900a36c24b364fef9d
Author: Brian Dewey <bdewey at gmail.com>
Date:   Tue Dec 29 23:33:57 2009 -0800

    Added support for in-buffer TODO keywords
---
 lib/org-ruby/headline.rb            |   20 +++++++++++++++-----
 lib/org-ruby/parser.rb              |   20 +++++++++++++++++++-
 spec/html_examples/custom-todo.html |   15 +++++++++++++++
 spec/html_examples/custom-todo.org  |   24 ++++++++++++++++++++++++
 spec/parser_spec.rb                 |   20 ++++++++++++++++++++
 5 files changed, 93 insertions(+), 6 deletions(-)

diff --git a/lib/org-ruby/headline.rb b/lib/org-ruby/headline.rb
index 4ead6a2..f49828c 100644
--- a/lib/org-ruby/headline.rb
+++ b/lib/org-ruby/headline.rb
@@ -30,7 +30,7 @@ module Orgmode
     # Special keywords allowed at the start of a line.
     Keywords = %w[TODO DONE]
 
-    KeywordsRegexp = Regexp.new("\\s*(#{Keywords.join('|')})\\s*")
+    KeywordsRegexp = Regexp.new("^(#{Keywords.join('|')})\$")
 
     def initialize(line, parser = nil)
       super(line, parser)
@@ -45,10 +45,7 @@ module Orgmode
           @headline_text.gsub!(TagsRegexp, "") # Removes the tags from the headline
         end
         @keyword = nil
-        if (@headline_text =~ KeywordsRegexp) then
-          @headline_text = $'
-          @keyword = $1
-        end
+        parse_keywords
       else
         raise "'#{line}' is not a valid headline"
       end
@@ -86,5 +83,18 @@ module Orgmode
       output << Line.to_html(@body_lines, opts)
       output
     end
+
+    ######################################################################
+    private
+
+    def parse_keywords
+      re = @parser.custom_keyword_regexp if @parser
+      re ||= KeywordsRegexp
+      words = @headline_text.split
+      if words.length > 0 && words[0] =~ re then
+        @keyword = words[0]
+        @headline_text.sub!(Regexp.new("^#{@keyword}\s*"), "")
+      end
+    end
   end                           # class Headline
 end                             # class Orgmode
diff --git a/lib/org-ruby/parser.rb b/lib/org-ruby/parser.rb
index 3cead4c..0e964b7 100644
--- a/lib/org-ruby/parser.rb
+++ b/lib/org-ruby/parser.rb
@@ -25,6 +25,15 @@ module Orgmode
     # This contains in-buffer options; a special case of in-buffer settings.
     attr_reader :options
 
+    # Array of custom keywords.
+    attr_reader :custom_keywords
+
+    # Regexp that recognizes words in custom_keywords.
+    def custom_keyword_regexp
+      return nil if @custom_keywords.empty?
+      Regexp.new("^(#{@custom_keywords.join('|')})\$")
+    end
+
     # Returns true if we are to export todo keywords on headings.
     def export_todo?
       "t" == @options["todo"]
@@ -75,7 +84,8 @@ module Orgmode
       else
         raise "Unsupported type for +lines+: #{lines.class}"
       end
-        
+
+      @custom_keywords = []
       @headlines = Array.new
       @current_headline = nil
       @header_lines = []
@@ -183,6 +193,14 @@ module Orgmode
             raise "Unexpected option: #{opt}"
           end
         end
+      elsif key =~ /^(TODO|SEQ_TODO|TYP_TODO)$/ then
+        # Handle todo keywords specially.
+        value.split.each do |keyword|
+          keyword.gsub!(/\(.*\)/, "") # Get rid of any parenthetical notes
+          keyword = Regexp.escape(keyword)
+          next if keyword == "\\|"      # Special character in the todo format, not really a keyword
+          @custom_keywords << keyword
+        end
       else
         @in_buffer_settings[key] = value
       end
diff --git a/spec/html_examples/custom-todo.html b/spec/html_examples/custom-todo.html
new file mode 100644
index 0000000..95dd5fe
--- /dev/null
+++ b/spec/html_examples/custom-todo.html
@@ -0,0 +1,15 @@
+<p class="title">custom-todo.org</p>
+<p>I copied this todo sequence from Worg. It shows a lot of power of the built-in todo functionality. Now, let’s make sure all of these are recognized (and therefore NOT exported.)</p>
+<h1><span class="todo-keyword TODO">TODO </span>Sample</h1>
+<ul>
+  <li>State “CANCELED”   from “INPROGRESS” [2009-12-29 Tue 22:26] \ I gave up.</li>
+  <li>State “WAITING”    from “”           [2009-12-29 Tue 22:25] \ huh?</li>
+</ul>
+<h1><span class="todo-keyword INPROGRESS">INPROGRESS </span>this one’s in progress</h1>
+<h1><span class="todo-keyword WAITING">WAITING </span>who am I waiting on?</h1>
+<h1><span class="todo-keyword DONE">DONE </span>Finished this one!</h1>
+<h1><span class="todo-keyword CANCELED">CANCELED </span>I gave up here.</h1>
+<h1>DONT be fooled by just a random word in all caps.</h1>
+<h1>todo <== this word doesn't match because the case doesn't match.</h1>
+<h1>TODOX</h1>
+<p>don’t be fooled by the initial substring above!</p>
diff --git a/spec/html_examples/custom-todo.org b/spec/html_examples/custom-todo.org
new file mode 100644
index 0000000..2ab839a
--- /dev/null
+++ b/spec/html_examples/custom-todo.org
@@ -0,0 +1,24 @@
+#+TITLE: custom-todo.org
+#+DESCRIPTION: Makes sure I can handle custom todo entries
+#+SEQ_TODO:   TODO(t) INPROGRESS(i) WAITING(w@) | DONE(d) CANCELED(c@)
+#+OPTIONS:    todo:t
+
+I copied this todo sequence from Worg. It shows a lot of power of the
+built-in todo functionality. Now, let's make sure all of these are
+recognized (and therefore NOT exported.)
+
+* TODO Sample
+
+  - State "CANCELED"   from "INPROGRESS" [2009-12-29 Tue 22:26] \\
+    I gave up.
+  - State "WAITING"    from ""           [2009-12-29 Tue 22:25] \\
+    huh?
+
+* INPROGRESS this one's in progress
+* WAITING who am I waiting on?
+* DONE Finished this one!
+* CANCELED I gave up here.
+* DONT be fooled by just a random word in all caps.
+* todo <== this word doesn't match because the case doesn't match.
+* TODOX
+  don't be fooled by the initial substring above!
diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb
index 1f3abe3..64c386f 100644
--- a/spec/parser_spec.rb
+++ b/spec/parser_spec.rb
@@ -92,6 +92,26 @@ describe Orgmode::Parser do
     p.export_tables?.should be_false
   end
 
+  # Custom keywords -- dynamic test cases
+  fname = File.join(File.dirname(__FILE__), %w[html_examples custom-todo.org])
+  p = Orgmode::Parser.load(fname)
+  puts p.custom_keyword_regexp.to_s
+  valid_keywords = %w[TODO INPROGRESS WAITING DONE CANCELED]
+  invalid_keywords = %w[TODOX todo inprogress Waiting done cANCELED NEXT |]
+  valid_keywords.each do |kw|
+    it "should match custom keyword #{kw}" do
+      (kw =~ p.custom_keyword_regexp).should be_true
+    end
+  end
+  invalid_keywords.each do |kw|
+    it "should not match custom keyword #{kw}" do
+      (kw =~ p.custom_keyword_regexp).should be_nil
+    end
+  end
+  it "should not match blank as a custom keyword" do
+    ("" =~ p.custom_keyword_regexp).should be_nil
+  end
+
   data_directory = File.join(File.dirname(__FILE__), "textile_examples")
   org_files = File.expand_path(File.join(data_directory, "*.org" ))
   files = Dir.glob(org_files)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-org.git



More information about the Pkg-ruby-extras-commits mailing list