Class: Kramdown::Converter::Latex

Inherits:
Base
  • Object
show all
Defined in:
lib/polytexnic/preprocessors/polytex.rb

Instance Method Summary collapse

Instance Method Details

#convert_a(el, opts) ⇒ Object

Overrides default convert_a. Unfortunately, kramdown is too aggressive in escaping characters in hrefs, converting

[foo bar](http://example.com/foo%20bar)

into

\href{http://example.com/foo\%20bar}{foo bar}

The ‘%20’ in the href then won’t work properly.



26
27
28
29
30
31
32
33
# File 'lib/polytexnic/preprocessors/polytex.rb', line 26

def convert_a(el, opts)
  url = el.attr['href']
  if url =~ /^#/
    "\\hyperlink{#{escape(url[1..-1])}}{#{inner(el, opts)}}"
  else
    "\\href{#{url}}{#{inner(el, opts)}}"
  end
end

#convert_codespan(el, opts) ⇒ Object

Converts ‘inline codespan`. This overrides kramdown’s default to use ‘kode` instead of `tt`.



15
16
17
# File 'lib/polytexnic/preprocessors/polytex.rb', line 15

def convert_codespan(el, opts)
  "\\kode{#{latex_link_target(el)}#{escape(el.value)}}"
end

#convert_standalone_image(el, opts, img) ⇒ Object

Uses figures for images only when label is present. This allows users to put raw (centered) images in their documents. The default behavior of kramdown is to wrap such images in a figure environment, which causes LaTeX to (a) treat them as floats and (b) include a caption. This may not be what the user wants, and it’s also nonstandard Markdown. On the other hand, it is really nice to be able to include captions using the default image syntax, so as a compromise we use Markdown behavior by default and kramdown behavior if the alt text contains a ‘label’ element.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/polytexnic/preprocessors/polytex.rb', line 44

def convert_standalone_image(el, opts, img)
  alt_text = el.children.first.attr['alt']
  if has_label?(alt_text)
    attrs = attribute_list(el)
    # Override the kramdown default by adding "here" placement.
    # Authors who want a different behavior can always use raw LaTeX.
    "\\begin{figure}[h]#{attrs}\n\\begin{center}\n#{img}\n\\end{center}\n\\caption{#{escape(el.children.first.attr['alt'])}}\n#{latex_link_target(el, true)}\n\\end{figure}#{attrs}\n"
  else
    img.gsub('\includegraphics', '\image') + "\n"
  end
end

#has_label?(text) ⇒ Boolean

Detects if text has a label.

Returns:

  • (Boolean)


57
58
59
# File 'lib/polytexnic/preprocessors/polytex.rb', line 57

def has_label?(text)
  text.include?($label_salt)
end