Skip to content

Commit

Permalink
Added some very basic support for images in LaTeX
Browse files Browse the repository at this point in the history
  • Loading branch information
Jase committed Aug 16, 2008
1 parent 3723cc3 commit a6f77ab
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
28 changes: 27 additions & 1 deletion lib/redcloth/formatters/latex.rb
Expand Up @@ -4,6 +4,15 @@ module RedCloth::Formatters::LATEX
include RedCloth::Formatters::Base

ENTITIES = YAML::load(File.read(File.dirname(__FILE__)+'/latex_entities.yml'))

module Settings
# Maps CSS style names to latex formatting options
def latex_image_styles
@latex_image_class_styles ||= {}
end
end

RedCloth::TextileDoc.send(:include, Settings)

def escape(text)
latex_esc(text)
Expand Down Expand Up @@ -136,8 +145,23 @@ def link(opts)
end

# FIXME: use includegraphics with security verification
#
# Remember to use '\RequirePackage{graphicx}' in your LaTeX header
#
# FIXME: Look at dealing with width / height gracefully as this should be
# specified in a unit like cm rather than px.
def image(opts)
""
# Don't know how to use remote links, plus can we trust them?
return "" if opts[:src] =~ /^\w+\:\/\//
# 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]",
" \\includegraphics[#{styling}]{#{opts[:src]}}",
(" \\caption{#{escape opts[:title]}}" if opts[:title]),
(" \\label{#{escape opts[:alt]}}" if opts[:alt]),
"\\end{figure}",
].compact.join "\n"
end

def footno(opts)
Expand Down Expand Up @@ -204,12 +228,14 @@ def dim(opts)

private

# Use this for block level commands that use \begin
def begin_chunk(type)
chunk_counter[type] += 1
return "\\begin{#{type}}" if 1 == chunk_counter[type]
''
end

# Use this for block level commands that use \end
def end_chunk(type)
chunk_counter[type] -= 1
raise RuntimeError, "Bad latex #{type} nesting detected" if chunk_counter[type] < 0 # This should never need to happen
Expand Down
4 changes: 2 additions & 2 deletions lib/redcloth/version.rb
Expand Up @@ -2,7 +2,7 @@ module RedCloth
module VERSION
MAJOR = 4
MINOR = 0
TINY = 1
TINY = 2
RELEASE_CANDIDATE = nil

STRING = [MAJOR, MINOR, TINY].join('.')
Expand All @@ -25,4 +25,4 @@ def ==(arg)
URL = "http://redcloth.org/"

DESCRIPTION = "#{NAME}-#{VERSION::FULL_VERSION} - Textile parser for Ruby.\n#{URL}"
end
end
32 changes: 32 additions & 0 deletions test/images.yml
@@ -1,12 +1,27 @@
---
in: This is an !image.jpg!
html: <p>This is an <img src="image.jpg" alt="" /></p>
latex: |+
This is an \begin{figure}[htp]
\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]
\includegraphics[]{image.jpg}
\caption{with alt text}
\end{figure}
---
in: This is an !http://example.com/i/image.jpg!
html: <p>This is an <img src="http://example.com/i/image.jpg" alt="" /></p>
# Note that we are removing remote links fro security reasons for now
latex: |+
This is an
---
in: This is an !http://example.com/i/image.jpg#a1!
html: <p>This is an <img src="http://example.com/i/image.jpg#a1" alt="" /></p>
Expand Down Expand Up @@ -176,6 +191,11 @@ html: <p>(This is an <a href="http://example.com/index.html?foo=bar&amp;a=b#a10"
name: image with relative src with dot
in: "!../../image.jpg!"
html: <p><img src="../../image.jpg" alt="" /></p>
latex: |+
\begin{figure}[htp]
\includegraphics[]{../../image.jpg}
\end{figure}
---
name: image with class
in: "!(myclass)image.jpg!"
Expand Down Expand Up @@ -204,10 +224,22 @@ html: <p><img src="image.jpg" style="color:red;" alt="" /></p>
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]
\includegraphics[]{/pictures/cat_and_fox.jpg}
\caption{Trady Blix \& The cartoon fox}
\end{figure}
---
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]
\includegraphics[]{/pictures/bacon.jpg}
\caption{The fox said: "Have some chunky bacon"}
\end{figure}
---
name: image attributes has single quote html entity in alt and title
in: "!/pictures/bacon.jpg(The fox said: 'Have some chunky bacon')!"
Expand Down

0 comments on commit a6f77ab

Please sign in to comment.