[DRE-commits] [ruby-org] 71/303: Support EXPORT_SELECT_TAGS and EXPORT_EXLUDE_TAGS for controlling parts of the tree to export

Jérémy Bobbio lunar at alioth.debian.org
Fri Aug 9 17:33:30 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 9690c7350c9d40e8f517a8c2d066956119bc297d
Author: Brian Dewey <bdewey at gmail.com>
Date:   Wed Dec 30 01:16:55 2009 -0800

    Support EXPORT_SELECT_TAGS and EXPORT_EXLUDE_TAGS for controlling parts of the tree to export
---
 History.txt                                 |    2 +
 lib/org-ruby/headline.rb                    |   12 ++++
 lib/org-ruby/parser.rb                      |   63 ++++++++++++++++++++
 spec/html_examples/export-exclude-only.html |   13 +++++
 spec/html_examples/export-exclude-only.org  |   81 ++++++++++++++++++++++++++
 spec/html_examples/export-tags.html         |    8 +++
 spec/html_examples/export-tags.org          |   82 +++++++++++++++++++++++++++
 spec/parser_spec.rb                         |    9 +++
 8 files changed, 270 insertions(+)

diff --git a/History.txt b/History.txt
index ea93d11..61862e7 100644
--- a/History.txt
+++ b/History.txt
@@ -8,6 +8,8 @@
   * Skipping text before the first headline (option skip:t)
   * Skipping tables (option |:nil)
   * Custom todo keywords
+  * EXPORT_SELECT_TAGS and EXPORT_EXLUDE_TAGS for controlling parts of
+    the tree to export
 * Rewrite "file:(blah).org" links to "http:(blah).html" links. This
   makes the inter-links to other org-mode files work.
 * Uses <th> tags inside table rows that precede table separators.
diff --git a/lib/org-ruby/headline.rb b/lib/org-ruby/headline.rb
index 70a222b..deaaae1 100644
--- a/lib/org-ruby/headline.rb
+++ b/lib/org-ruby/headline.rb
@@ -21,6 +21,15 @@ module Orgmode
     # Optional keyword found at the beginning of the headline.
     attr_reader :keyword
 
+    # Valid states for partial export.
+    # exclude::       The entire subtree from this heading should be excluded.
+    # headline_only:: The headline should be exported, but not the body.
+    # all::           Everything should be exported, headline/body/children.
+    ValidExportStates = [:exclude, :headline_only, :all]
+
+    # The export state of this headline. See +ValidExportStates+.
+    attr_accessor :export_state
+
     # This is the regex that matches a line
     LineRegexp = /^\*+\s+/
 
@@ -36,6 +45,7 @@ module Orgmode
       super(line, parser)
       @body_lines = []
       @tags = []
+      @export_state = :exclude
       if (@line =~ LineRegexp) then
         @level = $&.strip.length
         @headline_text = $'.strip
@@ -65,6 +75,7 @@ module Orgmode
     end
 
     def to_html(opts = {})
+      return "" if @export_state == :exclude
       if opts[:decorate_title]
         decoration = " class=\"title\""
         opts.delete(:decorate_title)
@@ -80,6 +91,7 @@ module Orgmode
         output << "<span class=\"todo-keyword #{@keyword}\">#{@keyword} </span>"
       end
       output << "#{escape(@headline_text)}</h#{@level}>\n"
+      return output if @export_state == :headline_only
       output << Line.to_html(@body_lines, opts)
       output
     end
diff --git a/lib/org-ruby/parser.rb b/lib/org-ruby/parser.rb
index 0e964b7..8de947a 100644
--- a/lib/org-ruby/parser.rb
+++ b/lib/org-ruby/parser.rb
@@ -34,6 +34,20 @@ module Orgmode
       Regexp.new("^(#{@custom_keywords.join('|')})\$")
     end
 
+    # A set of tags that, if present on any headlines in the org-file, means
+    # only those headings will get exported.
+    def export_select_tags
+      return Array.new unless @in_buffer_settings["EXPORT_SELECT_TAGS"]
+      @in_buffer_settings["EXPORT_SELECT_TAGS"].split
+    end
+
+    # A set of tags that, if present on any headlines in the org-file, means
+    # that subtree will not get exported.
+    def export_exclude_tags
+      return Array.new unless @in_buffer_settings["EXPORT_EXCLUDE_TAGS"]
+      @in_buffer_settings["EXPORT_EXCLUDE_TAGS"].split
+    end
+
     # Returns true if we are to export todo keywords on headings.
     def export_todo?
       "t" == @options["todo"]
@@ -156,6 +170,7 @@ module Orgmode
 
     # Converts the loaded org-mode file to HTML.
     def to_html
+      mark_trees_for_export
       @headline_number_stack = []
       export_options = { }
       export_options[:skip_tables] = true if not export_tables?
@@ -180,6 +195,54 @@ module Orgmode
     ######################################################################
     private
 
+    # Uses export_select_tags and export_exclude_tags to determine
+    # which parts of the org-file to export.
+    def mark_trees_for_export
+      marked_any = false
+      # cache the tags
+      select = export_select_tags
+      exclude = export_exclude_tags
+      inherit_export_level = nil
+      ancestor_stack = []
+
+      # First pass: See if any headlines are explicitly selected
+      @headlines.each do |headline|
+        ancestor_stack.pop while not ancestor_stack.empty? and headline.level <= ancestor_stack.last.level
+        if inherit_export_level and headline.level > inherit_export_level
+          headline.export_state = :all
+        else
+          inherit_export_level = nil
+          headline.tags.each do |tag|
+            if (select.include? tag) then
+              marked_any = true
+              headline.export_state = :all
+              ancestor_stack.each { |a| a.export_state = :headline_only unless a.export_state == :all }
+              inherit_export_level = headline.level
+            end
+          end
+        end
+        ancestor_stack.push headline
+      end
+
+      # If nothing was selected, then EVERYTHING is selected.
+      @headlines.each { |h| h.export_state = :all } unless marked_any
+
+      # Second pass. Look for things that should be excluded, and get rid of them.
+      @headlines.each do |headline|
+        if inherit_export_level and headline.level > inherit_export_level
+          headline.export_state = :exclude
+        else
+          inherit_export_level = nil
+          headline.tags.each do |tag|
+            if (exclude.include? tag) then
+              headline.export_state = :exclude
+              inherit_export_level = headline.level
+            end
+          end
+        end
+      end
+    end
+
     # Stores an in-buffer setting.
     def store_in_buffer_setting(key, value)
       if key == "OPTIONS" then
diff --git a/spec/html_examples/export-exclude-only.html b/spec/html_examples/export-exclude-only.html
new file mode 100644
index 0000000..3d828f8
--- /dev/null
+++ b/spec/html_examples/export-exclude-only.html
@@ -0,0 +1,13 @@
+<p class="title">export-headline-levels.org</p>
+<p>What happens when you exceed the number of headline levels to export?</p>
+<h1><span class="heading-number heading-number-1">1 </span>Headline 1</h1>
+<h2><span class="heading-number heading-number-2">1.1 </span>Headline 2</h2>
+<p>This bit of body <b>should</b> get exported.</p>
+<h3><span class="heading-number heading-number-3">1.1.1 </span>Headline 3</h3>
+<p>This bit of body gets exported.</p>
+<h4><span class="heading-number heading-number-4">1.1.1.1 </span>Headline 4 (include)</h4>
+<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo d [...]
+<h3><span class="heading-number heading-number-3">1.1.2 </span>Another headline 3</h3>
+<p>This one <b>should not</b> get exported!!</p>
+<h4><span class="heading-number heading-number-4">1.1.2.1 </span>Another headline 4</h4>
+<p>This also <b>cannot</b> get exported!!</p>
diff --git a/spec/html_examples/export-exclude-only.org b/spec/html_examples/export-exclude-only.org
new file mode 100644
index 0000000..64d7b0f
--- /dev/null
+++ b/spec/html_examples/export-exclude-only.org
@@ -0,0 +1,81 @@
+#+TITLE:     export-headline-levels.org
+#+AUTHOR:    
+#+EMAIL:     bdewey at gmail.com
+#+DATE:      2009-12-29 Tue
+#+DESCRIPTION: 
+#+KEYWORDS: 
+#+LANGUAGE:  en
+#+OPTIONS:   H:3 num:t toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
+#+OPTIONS:   TeX:t LaTeX:nil skip:nil d:nil todo:t pri:nil tags:not-in-toc
+#+INFOJS_OPT: view:nil toc:nil ltoc:t mouse:underline buttons:0 path:http://orgmode.org/org-info.js
+#+EXPORT_EXCLUDE_TAGS: exclude noexport
+#+TAGS: export noexport
+#+LINK_UP:   
+#+LINK_HOME: 
+
+What happens when you exceed the number of headline levels to export?
+
+* Shouldn't export                                     :noexport:
+
+  This text shouldn't be exported, right?
+
+** Nor this subheading?
+
+   Or its text?
+
+* Exlude me, too!                                       :exclude:
+
+* Headline 1                                        :READING:DVD:
+
+** Headline 2
+
+   This bit of body *should* get exported.
+
+*** Headline 3                                           :export:
+
+    This bit of body gets exported.
+
+**** Headline 4 (include)
+
+     Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
+     nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
+     erat, sed diam voluptua. At vero eos et accusam et justo duo
+     dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
+     sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit
+     amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
+     invidunt ut labore et dolore magna aliquyam erat, sed diam
+     voluptua. At vero eos et accusam et justo duo dolores et ea
+     rebum. Stet clita kasd gubergren, no sea takimata sanctus est
+     Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
+     consetetur sadipscing elitr, sed diam nonumy eirmod tempor
+     invidunt ut labore et dolore magna aliquyam erat, sed diam
+     voluptua. At vero eos et accusam et justo duo dolores et ea
+     rebum. Stet clita kasd gubergren, no sea takimata sanctus est
+     Lorem ipsum dolor sit amet.
+
+**** Headline 4 (exclude)                              :noexport:
+
+     Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
+     nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
+     erat, sed diam voluptua. At vero eos et accusam et justo duo
+     dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
+     sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit
+     amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
+     invidunt ut labore et dolore magna aliquyam erat, sed diam
+     voluptua. At vero eos et accusam et justo duo dolores et ea
+     rebum. Stet clita kasd gubergren, no sea takimata sanctus est
+     Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
+     consetetur sadipscing elitr, sed diam nonumy eirmod tempor
+     invidunt ut labore et dolore magna aliquyam erat, sed diam
+     voluptua. At vero eos et accusam et justo duo dolores et ea
+     rebum. Stet clita kasd gubergren, no sea takimata sanctus est
+     Lorem ipsum dolor sit amet.
+
+
+*** Another headline 3
+
+    This one *should not* get exported!!
+
+**** Another headline 4
+
+     This also *cannot* get exported!!
diff --git a/spec/html_examples/export-tags.html b/spec/html_examples/export-tags.html
new file mode 100644
index 0000000..96eb712
--- /dev/null
+++ b/spec/html_examples/export-tags.html
@@ -0,0 +1,8 @@
+<p class="title">export-headline-levels.org</p>
+<p>What happens when you exceed the number of headline levels to export?</p>
+<h1><span class="heading-number heading-number-1">1 </span>Headline 1</h1>
+<h2><span class="heading-number heading-number-2">1.1 </span>Headline 2</h2>
+<h3><span class="heading-number heading-number-3">1.1.1 </span>Headline 3</h3>
+<p>This bit of body gets exported.</p>
+<h4><span class="heading-number heading-number-4">1.1.1.1 </span>Headline 4 (include)</h4>
+<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo d [...]
diff --git a/spec/html_examples/export-tags.org b/spec/html_examples/export-tags.org
new file mode 100644
index 0000000..e4ab40b
--- /dev/null
+++ b/spec/html_examples/export-tags.org
@@ -0,0 +1,82 @@
+#+TITLE:     export-headline-levels.org
+#+AUTHOR:    
+#+EMAIL:     bdewey at gmail.com
+#+DATE:      2009-12-29 Tue
+#+DESCRIPTION: 
+#+KEYWORDS: 
+#+LANGUAGE:  en
+#+OPTIONS:   H:3 num:t toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
+#+OPTIONS:   TeX:t LaTeX:nil skip:nil d:nil todo:t pri:nil tags:not-in-toc
+#+INFOJS_OPT: view:nil toc:nil ltoc:t mouse:underline buttons:0 path:http://orgmode.org/org-info.js
+#+EXPORT_SELECT_TAGS: export
+#+EXPORT_EXCLUDE_TAGS: exclude noexport
+#+TAGS: export noexport
+#+LINK_UP:   
+#+LINK_HOME: 
+
+What happens when you exceed the number of headline levels to export?
+
+* Shouldn't export                                     :noexport:
+
+  This text shouldn't be exported, right?
+
+** Nor this subheading?
+
+   Or its text?
+
+* Exlude me, too!                                       :exclude:
+
+* Headline 1                                        :READING:DVD:
+
+** Headline 2
+
+   This bit of body should not get exported.
+
+*** Headline 3                                           :export:
+
+    This bit of body gets exported.
+
+**** Headline 4 (include)
+
+     Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
+     nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
+     erat, sed diam voluptua. At vero eos et accusam et justo duo
+     dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
+     sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit
+     amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
+     invidunt ut labore et dolore magna aliquyam erat, sed diam
+     voluptua. At vero eos et accusam et justo duo dolores et ea
+     rebum. Stet clita kasd gubergren, no sea takimata sanctus est
+     Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
+     consetetur sadipscing elitr, sed diam nonumy eirmod tempor
+     invidunt ut labore et dolore magna aliquyam erat, sed diam
+     voluptua. At vero eos et accusam et justo duo dolores et ea
+     rebum. Stet clita kasd gubergren, no sea takimata sanctus est
+     Lorem ipsum dolor sit amet.
+
+**** Headline 4 (exclude)                              :noexport:
+
+     Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
+     nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
+     erat, sed diam voluptua. At vero eos et accusam et justo duo
+     dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
+     sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit
+     amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
+     invidunt ut labore et dolore magna aliquyam erat, sed diam
+     voluptua. At vero eos et accusam et justo duo dolores et ea
+     rebum. Stet clita kasd gubergren, no sea takimata sanctus est
+     Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
+     consetetur sadipscing elitr, sed diam nonumy eirmod tempor
+     invidunt ut labore et dolore magna aliquyam erat, sed diam
+     voluptua. At vero eos et accusam et justo duo dolores et ea
+     rebum. Stet clita kasd gubergren, no sea takimata sanctus est
+     Lorem ipsum dolor sit amet.
+
+
+*** Another headline 3
+
+    This one *should not* get exported!!
+
+**** Another headline 4
+
+     This also *cannot* get exported!!
diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb
index a84456f..6c5d3c7 100644
--- a/spec/parser_spec.rb
+++ b/spec/parser_spec.rb
@@ -112,6 +112,15 @@ describe Orgmode::Parser do
     end
   end
 
+  describe "Custom include/exclude parser" do
+    fname = File.join(File.dirname(__FILE__), %w[html_examples export-tags.org])
+    p = Orgmode::Parser.load(fname)
+    it "should load tags" do
+      p.should have(2).export_exclude_tags
+      p.should have(1).export_select_tags
+    end
+  end
+
   describe "Export to Textile test cases" do
     data_directory = File.join(File.dirname(__FILE__), "textile_examples")
     org_files = File.expand_path(File.join(data_directory, "*.org" ))

-- 
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