Skip to content

Commit

Permalink
Limit overzealous superscript and subscript.
Browse files Browse the repository at this point in the history
Sup/sub phrases must be surrounded by spaces or square brackets. [#35 state:resolved]
  • Loading branch information
jgarber committed Aug 13, 2008
1 parent a882c98 commit 5a684d5
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
*Edge*

* Limit overzealous superscript and subscript. Sup/sub phrases must be surrounded by spaces or square brackets. #35

* Fixed HTML before tables causing the opening table tag to be emitted twice. #33

* Cleaned up unused code that was causing a warning. #28
Expand Down
12 changes: 9 additions & 3 deletions ext/redcloth_scan/redcloth_inline.rl
Expand Up @@ -29,6 +29,8 @@
footno = "[" >X %A digit+ %T "]" ;

# markup
begin_markup_phrase = " "? >A %{ STORE(beginning_space); };
end_markup_phrase = (" " | PUNCT | EOF | LF) @{ fhold; };
code = "["? "@" >X mtext >A %T :> "@" "]"? ;
code_tag_start = "<code" [^>]* ">" ;
code_tag_end = "</code>" ;
Expand All @@ -40,10 +42,12 @@
i = "["? "__" >X mtext >A %T :> "__" "]"? ;
del = "[-" >X C ( mtext ) >A %T :>> "-]" ;
emdash_parenthetical_phrase_with_spaces = " -- " mtext " -- " ;
del_phrase = ((" -") >X C ( mtext ) >A %T :>> ( "-" (" " | PUNCT) @{ fhold; } )) - emdash_parenthetical_phrase_with_spaces ;
del_phrase = (( " " >A %{ STORE(beginning_space); } "-") >X C ( mtext ) >A %T :>> ( "-" end_markup_phrase )) - emdash_parenthetical_phrase_with_spaces ;
ins = "["? "+" >X mtext >A %T :> "+" "]"? ;
sup = "["? "^" >X mtext >A %T :> "^" "]"? ;
sub = "["? "~" >X mtext >A %T :> "~" "]"? ;
sup = "[^" >X mtext >A %T :> "^]" ;
sup_phrase = ( begin_markup_phrase "^") >X C ( mtext ) >A %T :>> ( "^" end_markup_phrase ) ;
sub = "[~" >X mtext >A %T :> "~]" ;
sub_phrase = ( begin_markup_phrase "~") >X C ( mtext ) >A %T :>> ( "~" end_markup_phrase ) ;
span = "["? "%" >X mtext >A %T :> "%" "]"? ;
cite = "["? "??" >X mtext >A %T :> "??" "]"? ;
ignore = "["? "==" >X %A mtext %T :> "==" "]"? ;
Expand Down Expand Up @@ -102,7 +106,9 @@
del_phrase { PASS(block, text, del_phrase); };
ins { PARSE_ATTR(text); PASS(block, text, ins); };
sup { PARSE_ATTR(text); PASS(block, text, sup); };
sup_phrase { PARSE_ATTR(text); PASS(block, text, sup_phrase); };
sub { PARSE_ATTR(text); PASS(block, text, sub); };
sub_phrase { PARSE_ATTR(text); PASS(block, text, sub_phrase); };
span { PARSE_ATTR(text); PASS(block, text, span); };
cite { PARSE_ATTR(text); PASS(block, text, cite); };
ignore => ignore;
Expand Down
7 changes: 7 additions & 0 deletions lib/redcloth/formatters/base.rb
Expand Up @@ -34,6 +34,13 @@ def inline_redcloth_version(opts)
RedCloth::VERSION::STRING
end

[:del_phrase, :sup_phrase, :sub_phrase].each do |phrase_method|
method = phrase_method.to_s[0..2]
define_method(phrase_method) do |opts|
"#{opts[:beginning_space]}#{self.send(method, opts)}"
end
end

def method_missing(method, opts)
opts[:text] || ""
end
Expand Down
4 changes: 0 additions & 4 deletions lib/redcloth/formatters/html.rb
Expand Up @@ -52,10 +52,6 @@ def del(opts)
"<del#{pba(opts)}>#{opts[:text]}</del>"
end

def del_phrase(opts)
" #{del(opts)}"
end

[:ol, :ul].each do |m|
define_method("#{m}_open") do |opts|
opts[:block] = true
Expand Down
4 changes: 0 additions & 4 deletions lib/redcloth/formatters/latex.rb
Expand Up @@ -68,10 +68,6 @@ def escape_pre(text)
end
end

def del_phrase(opts)
" #{del(opts)}"
end

{ :ol => 'enumerate',
:ul => 'itemize',
}.each do |m, env|
Expand Down
5 changes: 5 additions & 0 deletions test/basic.yml
Expand Up @@ -265,6 +265,11 @@ name: subscript
desc: To subscript, surround with tildes.
in: log ~2~ x
html: <p>log <sub>2</sub> x</p>
---
name: tight superscript and subscript
desc: if you want your superscript or subscript to not be surrounded by spaces, you must use square brackets
in: f(x, n) = log[~4~]x[^n^]
html: '<p>f(x, n) = log<sub>4</sub>x<sup>n</sup></p>'
---
name: span
desc: Lastly, if you find yourself needing to customize the style of a passage, use percent symbols to translate the passage as an HTML span.
Expand Down
6 changes: 5 additions & 1 deletion test/html.yml
Expand Up @@ -292,4 +292,8 @@ html: |-
<td>e</td>
<td>f</td>
</tr>
</table>
</table>
---
name: tilde in innerHTML is not altered
in: '<a href="http://foo.com/bar?something=1~2~3">http://foo.com/bar?something=1~2~3</a>'
html: '<p><a href="http://foo.com/bar?something=1~2~3">http://foo.com/bar?something=1~2~3</a></p>'

0 comments on commit 5a684d5

Please sign in to comment.