Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:jgarber/redcloth
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarber committed Mar 9, 2009
2 parents df84e47 + 2ae0130 commit 829c45e
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 56 deletions.
38 changes: 24 additions & 14 deletions lib/redcloth/formatters/latex.rb
Expand Up @@ -15,11 +15,11 @@ def latex_image_styles
RedCloth::TextileDoc.send(:include, Settings)

# headers
{ :h1 => 'section*',
:h2 => 'subsection*',
:h3 => 'subsubsection*',
:h4 => 'textbf',
:h5 => 'textbf',
{ :h1 => 'section',
:h2 => 'subsection',
:h3 => 'subsubsection',
:h4 => 'paragraph',
:h5 => 'subparagraph',
:h6 => 'textbf',
}.each do |m,tag|
define_method(m) do |opts|
Expand All @@ -41,9 +41,15 @@ def latex_image_styles
"\\#{tag}{#{opts[:text]}}"
end
end

# inline verbatim
define_method(:code) do |opts|
opts[:block] ? opts[:text] : "\\verb@#{opts[:text]}@"
end

{ :sup => '\ensuremath{^\textrm{#1}}',
:sub => '\ensuremath{_\textrm{#1}}',
# sub/superscripts
{ :sup => '\textsuperscript{#1}',
:sub => '\textsubscript{#1}',
}.each do |m, expr|
define_method(m) do |opts|
expr.sub('#1', opts[:text])
Expand All @@ -52,7 +58,6 @@ def latex_image_styles

# environments
{ :pre => 'verbatim',
:code => 'verbatim',
:cite => 'quote',
}.each do |m, env|
define_method(m) do |opts|
Expand Down Expand Up @@ -82,13 +87,13 @@ def latex_image_styles
end

def li_open(opts)
"#{li_close unless opts.delete(:first)}\t\\item #{opts[:text]}"
"#{li_close unless opts.delete(:first)} \\item #{opts[:text]}"
end

def li_close(opts=nil)
"\n"
end

def p(opts)
opts[:text] + "\n\n"
end
Expand Down Expand Up @@ -131,12 +136,16 @@ def table_open(opts)
return ""
end

# FIXME: need caption and label elements similar to image -> figure
def table_close(opts)
output = "\\begin{tabular}{ #{"l " * @table[0].size }}\n"
output = "\\begin{table}\n"
output << " \\centering\n"
output << " \\begin{tabular}{ #{"l " * @table[0].size }}\n"
@table.each do |row|
output << " #{row.join(" & ")} \\\\\n"
output << " #{row.join(" & ")} \\\\\n"
end
output << "\\end{tabular}\n"
output << " \\end{tabular}\n"
output << "\\end{table}\n"
output
end

Expand Down Expand Up @@ -174,7 +183,8 @@ def image(opts)
# Resolve CSS styles if any have been set
styling = opts[:class].to_s.split(/\s+/).collect { |style| latex_image_styles[style] }.compact.join ','
# Build latex code
[ "\\begin{figure}[htp]",
[ "\\begin{figure}",
" \\centering",
" \\includegraphics[#{styling}]{#{opts[:src]}}",
(" \\caption{#{escape opts[:title]}}" if opts[:title]),
(" \\label{#{escape opts[:alt]}}" if opts[:alt]),
Expand Down
15 changes: 15 additions & 0 deletions test/code.yml
Expand Up @@ -2,24 +2,31 @@
name: inline code
in: 'This is an empty dictionary: @{}@'
html: '<p>This is an empty dictionary: <code>{}</code></p>'
latex: "This is an empty dictionary: \\verb@{}@\n\n"
---
name: inline code escapement
in: 'Please type @cat "file.txt" > otherfile.txt@ at the prompt.'
html: '<p>Please type <code>cat "file.txt" &gt; otherfile.txt</code> at the prompt.</p>'
latex: "Please type \\verb@cat \"file.txt\" > otherfile.txt@ at the prompt.\n\n"
---
name: inline code escapement with digits
in: |-
Regex-based string substitution with Ruby's gsub!: @"123<789".gsub!(/</, "") => "123789"@
html: |-
<p>Regex-based string substitution with Ruby&#8217;s gsub!: <code>"123&lt;789".gsub!(/&lt;/, "") =&gt; "123789"</code></p>
latex: |+
Regex-based string substitution with Ruby's gsub!: \verb@"123<789".gsub!(/</, "") => "123789"@
---
name: inlne code escapement describing textile paragraph styling
in: 'This paragraph is aligned left but if you add this: @p>.@ to the beginning it will be aligned right.'
html: '<p>This paragraph is aligned left but if you add this: <code>p&gt;.</code> to the beginning it will be aligned right.</p>'
latex: "This paragraph is aligned left but if you add this: \\verb@p>.@ to the beginning it will be aligned right.\n\n"
---
name: escapes code snippet containing html tag
in: 'At the top of each page, please put @<h2>Title</h2>@ in the HTML.'
html: '<p>At the top of each page, please put <code>&lt;h2&gt;Title&lt;/h2&gt;</code> in the <span class="caps">HTML</span>.</p>'
latex: "At the top of each page, please put \\verb@<h2>Title</h2>@ in the \\MakeUppercase{HTML}.\n\n"
---
name: escaping in blockcode
in: 'bc. This is within a block of code, so < and > should be entities. You can talk about a <p class="foo"> tag if you wish and it will be properly escaped.'
Expand Down Expand Up @@ -127,6 +134,13 @@ html: |-
<li><code>bar</code></li>
<li>and <code>x</code> is also.</li>
</ul>
latex: |+
\begin{itemize}
\item \verb@foo@
\item \verb@bar@
\item and \verb@x@ is also.
\end{itemize}
---
name: extended block code
in: |-
Expand Down Expand Up @@ -226,3 +240,4 @@ valid_html: false
name: code containing parentheses
in: 'p. @some_method(some_params, some => test);@ Oh dear this fails'
html: '<p><code>some_method(some_params, some =&gt; test);</code> Oh dear this fails</p>'
latex: "\\verb@some_method(some_params, some => test);@ Oh dear this fails\n\n"
17 changes: 11 additions & 6 deletions test/images.yml
Expand Up @@ -2,15 +2,17 @@
in: This is an !image.jpg!
html: <p>This is an <img src="image.jpg" alt="" /></p>
latex: |+
This is an \begin{figure}[htp]
This is an \begin{figure}
\centering
\includegraphics[]{image.jpg}
\end{figure}
---
in: This is an !image.jpg(with alt text)!
html: <p>This is an <img src="image.jpg" title="with alt text" alt="with alt text" /></p>
latex: |+
This is an \begin{figure}[htp]
This is an \begin{figure}
\centering
\includegraphics[]{image.jpg}
\caption{with alt text}
\end{figure}
Expand Down Expand Up @@ -192,7 +194,8 @@ name: image with relative src with dot
in: "!../../image.jpg!"
html: <p><img src="../../image.jpg" alt="" /></p>
latex: |+
\begin{figure}[htp]
\begin{figure}
\centering
\includegraphics[]{../../image.jpg}
\end{figure}
Expand Down Expand Up @@ -225,7 +228,8 @@ name: image attributes has ampersand html entity in alt and title
in: "!/pictures/cat_and_fox.jpg(Trady Blix & The cartoon fox)!"
html: '<p><img src="/pictures/cat_and_fox.jpg" title="Trady Blix &amp; The cartoon fox" alt="Trady Blix &amp; The cartoon fox" /></p>'
latex: |+
\begin{figure}[htp]
\begin{figure}
\centering
\includegraphics[]{/pictures/cat_and_fox.jpg}
\caption{Trady Blix \& The cartoon fox}
\end{figure}
Expand All @@ -235,7 +239,8 @@ name: image attributes has double quote html entity in alt and title
in: '!/pictures/bacon.jpg(The fox said: "Have some chunky bacon")!'
html: '<p><img src="/pictures/bacon.jpg" title="The fox said: &quot;Have some chunky bacon&quot;" alt="The fox said: &quot;Have some chunky bacon&quot;" /></p>'
latex: |+
\begin{figure}[htp]
\begin{figure}
\centering
\includegraphics[]{/pictures/bacon.jpg}
\caption{The fox said: "Have some chunky bacon"}
\end{figure}
Expand Down Expand Up @@ -266,4 +271,4 @@ html: |-
name: with link that contains parentheses
in: "!image.jpg(Alt text with (parentheses).)!"
html: |-
<p><img src="image.jpg" title="Alt text with (parentheses)." alt="Alt text with (parentheses)." /></p>
<p><img src="image.jpg" title="Alt text with (parentheses)." alt="Alt text with (parentheses)." /></p>
58 changes: 35 additions & 23 deletions test/table.yml
Expand Up @@ -19,11 +19,14 @@ html: |-
</table>
<h3>A header after the table</h3>
latex: |+
\begin{tabular}{ l l l }
a & b & c \\
1 & 2 & 3 \\
\end{tabular}
\subsubsection*{A header after the table}
\begin{table}
\centering
\begin{tabular}{ l l l }
a & b & c \\
1 & 2 & 3 \\
\end{tabular}
\end{table}
\subsubsection{A header after the table}
---
in: |
Expand Down Expand Up @@ -64,10 +67,13 @@ html: |-
</tr>
</table>
latex: |+
\begin{tabular}{ l l l l l }
This & is & a & simple & table \\
This & is & a & simple & row \\
\end{tabular}
\begin{table}
\centering
\begin{tabular}{ l l l l l }
This & is & a & simple & table \\
This & is & a & simple & row \\
\end{tabular}
\end{table}
---
in: |-
table{border:1px solid black}.
Expand Down Expand Up @@ -183,13 +189,16 @@ html: |-
</tr>
</table>
latex: |+
\begin{tabular}{ l l }
Cell with gray background & Normal cell \\
\multicolumn{2}{ l l }{Cell spanning 2 columns} \\
\multirow{2}{*}{Cell spanning 2 rows} & one \\
& two \\
Right-aligned cell & Left-aligned cell \\
\end{tabular}
\begin{table}
\centering
\begin{tabular}{ l l }
Cell with gray background & Normal cell \\
\multicolumn{2}{ l l }{Cell spanning 2 columns} \\
\multirow{2}{*}{Cell spanning 2 rows} & one \\
& two \\
Right-aligned cell & Left-aligned cell \\
\end{tabular}
\end{table}
---
name: row spanning mid-row
in: |-
Expand Down Expand Up @@ -225,13 +234,16 @@ html: |-
</tr>
</table>
latex: |+
\begin{tabular}{ l l l }
1 & 2 & 3 \\
1 & \multirow{3}{*}{2} & 3 \\
1 & & 3 \\
1 & & 3 \\
1 & 2 & 3 \\
\end{tabular}
\begin{table}
\centering
\begin{tabular}{ l l l }
1 & 2 & 3 \\
1 & \multirow{3}{*}{2} & 3 \\
1 & & 3 \\
1 & & 3 \\
1 & 2 & 3 \\
\end{tabular}
\end{table}
---
in: |
{background:#ddd}. |S|Target|Complete|App|Milestone|
Expand Down
50 changes: 37 additions & 13 deletions test/textism.yml
Expand Up @@ -2,17 +2,32 @@
name: header one
in: h1. Header 1
html: <h1>Header 1</h1>
latex: "\section*{Header 1}\n\n"
latex: "\\section{Header 1}\n\n"
---
name: header two
in: h2. Header 2
html: <h2>Header 2</h2>
latex: "\subsection*{Header 2}\n\n"
latex: "\\subsection{Header 2}\n\n"
---
name: header three
in: h3. Header 3
html: <h3>Header 3</h3>
latex: "\subsubsection*{Header 3}\n\n"
latex: "\\subsubsection{Header 3}\n\n"
---
name: header four
in: h4. Header 4
html: <h4>Header 4</h4>
latex: "\\paragraph{Header 4}\n\n"
---
name: header five
in: h5. Header 5
html: <h5>Header 5</h5>
latex: "\\subparagraph{Header 5}\n\n"
---
name: header six
in: h6. Header 6
html: <h6>Header 6</h6>
latex: "\\textbf{Header 6}\n\n"
---
name: blockquote
in: |-
Expand Down Expand Up @@ -54,10 +69,10 @@ html: |-
</ol>
latex: |+
\begin{enumerate}
\item A first item
\item A second item
\item A third item
\item A fourth item
\item A first item
\item A second item
\item A third item
\item A fourth item
\end{enumerate}
---
Expand All @@ -76,10 +91,10 @@ html: |-
</ul>
latex: |+
\begin{itemize}
\item A first item
\item A second item
\item A third item
\item A fourth item
\item A first item
\item A second item
\item A third item
\item A fourth item
\end{itemize}
---
Expand Down Expand Up @@ -129,10 +144,11 @@ latex: "\\underline{a phrase}\n\n"
---
in: ^a phrase^
html: <p><sup>a phrase</sup></p>
latex: "\\ensuremath{^\\textrm{a phrase}}\n\n"
latex: "\\textsuperscript{a phrase}\n\n"
---
in: ~a phrase~
html: <p><sub>a phrase</sub></p>
latex: "\\textsubscript{a phrase}\n\n"
---
in: %(myclass)SPAN%
html: <p><span class="myclass"><span class="caps">SPAN</span></span></p>
Expand Down Expand Up @@ -188,6 +204,14 @@ html: |-
<td>row</td>
</tr>
</table>
latex: |
\begin{table}
\centering
\begin{tabular}{ l l l l l }
This & is & a & simple & table \\
This & is & a & simple & row \\
\end{tabular}
\end{table}
---
in: |-
table{border:1px solid black}.
Expand Down Expand Up @@ -477,4 +501,4 @@ html: |-
---
name: copyright symbol at line start
in: "(C) copyright conversion (C) test."
html: "<p>&#169; copyright conversion &#169; test.</p>"
html: "<p>&#169; copyright conversion &#169; test.</p>"

0 comments on commit 829c45e

Please sign in to comment.