diff --git a/lib/gollum/frontend/views/page.rb b/lib/gollum/frontend/views/page.rb index 84246a9d..d232c646 100644 --- a/lib/gollum/frontend/views/page.rb +++ b/lib/gollum/frontend/views/page.rb @@ -79,6 +79,17 @@ module Precious def mathjax @mathjax end + + # Access to embedded metadata. + # + # Examples + # + # {{#metadata}}{{name}}{{/metadata}} + # + # Returns Hash. + def metadata + @page.metadata + end end end end diff --git a/lib/gollum/markup.rb b/lib/gollum/markup.rb index aec95c81..2d6ba686 100644 --- a/lib/gollum/markup.rb +++ b/lib/gollum/markup.rb @@ -7,6 +7,8 @@ module Gollum class Markup attr_accessor :toc + attr_reader :metadata + # Initialize a new Markup object. # # page - The Gollum::Page. @@ -27,6 +29,7 @@ module Gollum @wsdmap = {} @premap = {} @toc = nil + @metadata = nil end # Render the content with Gollum wiki syntax on top of the file's own @@ -43,6 +46,7 @@ module Gollum @wiki.sanitizer data = @data.dup + data = extract_metadata(data) data = extract_code(data) data = extract_tex(data) data = extract_wsd(data) @@ -565,6 +569,29 @@ module Gollum data end + ######################################################################### + # + # Metadata + # + ######################################################################### + + # Extract metadata for data and build metadata table. Metadata + # is content found between `` markers, and must + # be a valid YAML mapping. + # + # Returns the String of formatted data with metadata removed. + def extract_metadata(data) + @metadata ||= {} + data.gsub(/\<\!--+\s+---(.*?)--+\>/m) do + yaml = @wiki.sanitizer.clean($1) + hash = YAML.load(yaml) + if Hash === hash + @metadata.update(hash) + end + '' + end + end + # Hook for getting the formatted value of extracted tag data. # # type - Symbol value identifying what type of data is being extracted. diff --git a/lib/gollum/page.rb b/lib/gollum/page.rb index 5c8f6ff6..aae9ca32 100644 --- a/lib/gollum/page.rb +++ b/lib/gollum/page.rb @@ -217,6 +217,14 @@ module Gollum markup_class.toc end + # Public: Embedded metadata. + # + # Returns Hash of metadata. + def meta_data() + formatted_data if markup_class.metadata == nil + markup_class.metadata + end + # Public: The format of the page. # # Returns the Symbol format of the page. One of: diff --git a/test/test_markup.rb b/test/test_markup.rb index cfd78fd2..a0013bf9 100644 --- a/test/test_markup.rb +++ b/test/test_markup.rb @@ -517,6 +517,57 @@ np.array([[2,2],[1,3]],np.float) assert_not_nil rendered.match(output) end + ######################################################################### + # + # Metadata Blocks + # + ######################################################################### + + test "metadata blocks" do + content = "a\n\n\n\nb" + output = "

a

\n\n

b

" + result = {'tags'=>['foo','bar']} + + index = @wiki.repo.index + index.add("Bilbo-Baggins.md", content) + index.commit("Add metadata") + + page = @wiki.page("Bilbo Baggins") + rendered = Gollum::Markup.new(page).render + assert_equal output, rendered + assert_equal result, page.meta_data + end + + test "metadata blocks with newline" do + content = "a\n\n\n\nb" + output = "

a

\n\n

b

" + result = {'tags'=>['foo','bar']} + + index = @wiki.repo.index + index.add("Bilbo-Baggins.md", content) + index.commit("Add metadata") + + page = @wiki.page("Bilbo Baggins") + rendered = Gollum::Markup.new(page).render + assert_equal output, rendered + assert_equal result, page.meta_data + end + + test "metadata sanitation" do + content = "a\n\n\n\nb" + output = "

a

\n\n

b

" + result = {'foo'=>nil} + + index = @wiki.repo.index + index.add("Bilbo-Baggins.md", content) + index.commit("Add metadata") + + page = @wiki.page("Bilbo Baggins") + rendered = Gollum::Markup.new(page).render + assert_equal output, rendered + assert_equal result, page.meta_data + end + ######################################################################### # # Various