Automatic Theorem Numbering
Can now refer to numbered theorems by \ref{...}, as in LaTeX
This commit is contained in:
parent
da81a2fbdb
commit
2fb41f12ce
7 changed files with 72 additions and 14 deletions
29
vendor/plugins/maruku/lib/maruku/ext/div.rb
vendored
29
vendor/plugins/maruku/lib/maruku/ext/div.rb
vendored
|
@ -70,9 +70,8 @@ MaRuKu::In::Markdown::register_block_extension(
|
|||
if al_string =~ /^\{(.*)\}\s*$/
|
||||
inside = $1
|
||||
cs = MaRuKu::In::Markdown::SpanLevelParser::CharSource
|
||||
# al = al_string &&
|
||||
# doc.read_attribute_list(cs.new(inside), its_context=nil, break_on=[nil])
|
||||
al = doc.read_attribute_list(cs.new(inside), its_context=nil, break_on=[nil])
|
||||
al = al_string &&
|
||||
doc.read_attribute_list(cs.new(inside), its_context=nil, break_on=[nil])
|
||||
end
|
||||
|
||||
src = MaRuKu::In::Markdown::BlockLevelParser::LineSource.new(lines)
|
||||
|
@ -85,8 +84,28 @@ MaRuKu::In::Markdown::register_block_extension(
|
|||
|
||||
module MaRuKu; class MDElement
|
||||
|
||||
def md_div(children, a=nil)
|
||||
self.md_el(:div, children, meta={}, a)
|
||||
def md_div(children, al=nil)
|
||||
type = label = num = nil
|
||||
doc.refid2ref ||= {}
|
||||
if al
|
||||
al.each do |k, v|
|
||||
case k
|
||||
when :class
|
||||
type = $1 if v =~ /^num_(\w*)/
|
||||
when :id
|
||||
label = v
|
||||
end
|
||||
end
|
||||
end
|
||||
if type
|
||||
doc.refid2ref[type] ||= {}
|
||||
num = doc.refid2ref[type].length + 1 || 1
|
||||
end
|
||||
e = self.md_el(:div, children, meta={:label => label, :type => type, :num => num}, al)
|
||||
if type && label
|
||||
doc.refid2ref[type].update({label => e})
|
||||
end
|
||||
e
|
||||
end
|
||||
|
||||
end end
|
||||
|
|
|
@ -2,7 +2,7 @@ module MaRuKu
|
|||
|
||||
class MDDocument
|
||||
# Hash equation id (String) to equation element (MDElement)
|
||||
attr_accessor :eqid2eq
|
||||
attr_accessor :eqid2eq, :refid2ref
|
||||
|
||||
def is_math_enabled?
|
||||
get_setting :math_enabled
|
||||
|
@ -103,3 +103,17 @@ end
|
|||
true
|
||||
}
|
||||
)
|
||||
|
||||
# This adds support for \ref
|
||||
RegRef = /\\ref\{(\w*)\}/
|
||||
MaRuKu::In::Markdown::register_span_extension(
|
||||
:chars => [?\\, ?(],
|
||||
:regexp => RegRef,
|
||||
:handler => lambda { |doc, src, con|
|
||||
return false if not doc.is_math_enabled?
|
||||
refid = src.read_regexp(RegRef).captures.compact.first
|
||||
r = doc.md_el(:divref, [], meta={:refid=>refid})
|
||||
con.push r
|
||||
true
|
||||
}
|
||||
)
|
||||
|
|
|
@ -165,6 +165,23 @@ module MaRuKu; module Out; module HTML
|
|||
end
|
||||
end
|
||||
|
||||
def to_html_divref
|
||||
ref= nil
|
||||
self.doc.refid2ref.each_value { |h|
|
||||
ref = h[self.refid] if h.has_key?(self.refid)
|
||||
}
|
||||
if ref
|
||||
num = ref.num
|
||||
a = Element.new 'a'
|
||||
a.attributes['class'] = 'maruku-ref'
|
||||
a.attributes['href'] = "#" + self.refid
|
||||
a << Text.new(num.to_s)
|
||||
a
|
||||
else
|
||||
maruku_error "Cannot find div #{self.refid.inspect}"
|
||||
Text.new "\\ref{#{self.refid}}"
|
||||
end
|
||||
end
|
||||
|
||||
end end end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue