Merge pull request #419 from trans/metadata
Add support for embedded page metadata.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 `<!-- ---` and `-->` 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.
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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<!-- ---\ntags: [foo, bar]\n-->\n\nb"
|
||||
output = "<p>a</p>\n\n<p>b</p>"
|
||||
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---\ntags: [foo, bar]\n-->\n\nb"
|
||||
output = "<p>a</p>\n\n<p>b</p>"
|
||||
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<!-- ---\nfoo: <script>alert('');</script>\n-->\n\nb"
|
||||
output = "<p>a</p>\n\n<p>b</p>"
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user