Add system for registering new page extensions / markup formats

This commit is contained in:
James Dabbs
2012-10-17 21:17:58 -04:00
committed by James Dabbs
parent 982915a22f
commit 62601f1adc
6 changed files with 69 additions and 59 deletions
+1
View File
@@ -19,6 +19,7 @@ require File.expand_path('../gollum/page', __FILE__)
require File.expand_path('../gollum/file', __FILE__)
require File.expand_path('../gollum/file_view', __FILE__)
require File.expand_path('../gollum/markup', __FILE__)
require File.expand_path('../gollum/markups', __FILE__)
require File.expand_path('../gollum/sanitization', __FILE__)
require File.expand_path('../gollum/web_sequence_diagram', __FILE__)
require File.expand_path('../gollum/frontend/uri_encode_component', __FILE__)
+2 -2
View File
@@ -1,8 +1,8 @@
module Precious
module Editable
def formats(selected = @page.format)
Gollum::Page::FORMAT_NAMES.map do |key, val|
{ :name => val,
Gollum::Markup.formats.map do |key, val|
{ :name => val[:name],
:id => key.to_s,
:selected => selected == key}
end.sort do |a, b|
+22
View File
@@ -15,6 +15,28 @@ module Gollum
class Markup
include Precious::Helpers
@formats = {}
class << self
attr_reader :formats
# Register a file extension and associated markup type
#
# ext - The file extension
# name - The name of the markup type
# options - Hash of options:
# regexp - Regexp to match against.
# Defaults to exact match of ext.
#
# If given a block, that block will be registered with GitHub::Markup to
# render any matching pages
def register(ext, name, options = {}, &block)
regexp = options[:regexp] || Regexp.new(ext.to_s)
@formats[ext] = { :name => name, :regexp => regexp }
GitHub::Markup.add_markup(regexp, &block) if block_given?
end
end
attr_accessor :toc
attr_reader :metadata
+13
View File
@@ -0,0 +1,13 @@
module Gollum
class Markup
register(:markdown, "Markdown", :regexp => /md|mkdn?|mdown|markdown/)
register(:textile, "Textile")
register(:rdoc, "RDoc")
register(:org, "Org-mode")
register(:creole, "Creole")
register(:rest, "reStructuredText", :regexp => /re?st(\.txt)?/)
register(:asciidoc, "AsciiDoc")
register(:mediawiki, "MediaWiki", :regexp => /(media)?wiki/)
register(:pod, "Pod")
end
end
+22 -54
View File
@@ -5,17 +5,6 @@ module Gollum
Wiki.page_class = self
VALID_PAGE_RE = /^(.+)\.(md|mkdn?|mdown|markdown|textile|rdoc|org|creole|re?st(\.txt)?|asciidoc|pod|(media)?wiki)$/i
FORMAT_NAMES = { :markdown => "Markdown",
:textile => "Textile",
:rdoc => "RDoc",
:org => "Org-mode",
:creole => "Creole",
:rest => "reStructuredText",
:asciidoc => "AsciiDoc",
:mediawiki => "MediaWiki",
:pod => "Pod" }
# Sets a Boolean determing whether this page is a historical version.
#
# Returns nothing.
@@ -26,13 +15,29 @@ module Gollum
# Returns a Page
attr_accessor :parent_page
# Checks if a filename has a valid extension understood by GitHub::Markup.
# Checks a filename against the registered markup extensions
#
# filename - String filename, like "Home.md"
#
# Returns e.g. ["Home", :markdown], or [] if the extension is unregistered
def self.parse_filename(filename)
return [] unless filename =~ /^(.+)\.([a-zA-Z]\w*)$/i
pref, ext = $1, $2
Gollum::Markup.formats.each_pair do |name, format|
return [pref, name] if ext =~ format[:regexp]
end
[]
end
# Checks if a filename has a valid, registered extension
#
# filename - String filename, like "Home.md".
#
# Returns the matching String basename of the file without the extension.
def self.valid_filename?(filename)
filename && filename.to_s =~ VALID_PAGE_RE && $1
self.parse_filename(filename).first
end
# Checks if a filename has a valid extension understood by GitHub::Markup.
@@ -51,34 +56,9 @@ module Gollum
#
# filename - The String filename.
#
# Returns the Symbol format of the page. One of:
# [ :markdown | :textile | :rdoc | :org | :rest | :asciidoc | :pod |
# :roff ]
# Returns the Symbol format of the page; one of the registered format types
def self.format_for(filename)
case filename.to_s
when /\.(md|mkdn?|mdown|markdown)$/i
:markdown
when /\.(textile)$/i
:textile
when /\.(rdoc)$/i
:rdoc
when /\.(org)$/i
:org
when /\.(creole)$/i
:creole
when /\.(re?st(\.txt)?)$/i
:rest
when /\.(asciidoc)$/i
:asciidoc
when /\.(pod)$/i
:pod
when /\.(\d)$/i
:roff
when /\.(media)?wiki$/i
:mediawiki
else
nil
end
self.parse_filename(filename).last
end
# Reusable filter to turn a filename (without path) into a canonical name.
@@ -249,9 +229,7 @@ module Gollum
# Public: The format of the page.
#
# Returns the Symbol format of the page. One of:
# [ :markdown | :textile | :rdoc | :org | :rest | :asciidoc | :pod |
# :roff ]
# Returns the Symbol format of the page; one of the registered format types
def format
self.class.format_for(@blob.name)
end
@@ -359,17 +337,7 @@ module Gollum
#
# Returns the String extension (no leading period).
def self.format_to_ext(format)
case format
when :markdown then 'md'
when :textile then 'textile'
when :rdoc then 'rdoc'
when :org then 'org'
when :creole then 'creole'
when :rest then 'rest'
when :asciidoc then 'asciidoc'
when :pod then 'pod'
when :mediawiki then 'mediawiki'
end
format == :markdown ? "md" : format.to_s
end
#########################################################################