Class: Texstylist
- Inherits:
-
Object
- Object
- Texstylist
- Defined in:
- lib/texstylist.rb
Constant Summary collapse
- @@default_package_selection =
%w( graphicx grffile latexsym textcomp longtable multirow booktabs ams natbib url hyperref latexml inputenc babel)
- @@default_package_options =
{'grffile' => ['space'], 'inputenc' => ['utf8']}
Instance Attribute Summary collapse
-
#style ⇒ Object
Returns the value of attribute style.
Instance Method Summary collapse
-
#initialize(style = :authorea, package_candidates = @@default_package_selection) ⇒ Texstylist
constructor
A new instance of Texstylist.
- #render(body, header = nil, metadata = {}) ⇒ Object
- #simplify_latex(text) ⇒ Object
- #stylize_citations(article, metadata) ⇒ Object
Constructor Details
#initialize(style = :authorea, package_candidates = @@default_package_selection) ⇒ Texstylist
Returns a new instance of Texstylist.
12 13 14 15 16 |
# File 'lib/texstylist.rb', line 12 def initialize(style = :authorea, package_candidates = @@default_package_selection) @style = Texstyles::Style.new(style) # setup default packages @default_packages_list = package_candidates.select{|candidate| @style.package_compatible?(candidate)} end |
Instance Attribute Details
#style ⇒ Object
Returns the value of attribute style.
6 7 8 |
# File 'lib/texstylist.rb', line 6 def style @style end |
Instance Method Details
#render(body, header = nil, metadata = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/texstylist.rb', line 18 def render(body, header=nil, = {}) return '' if body.empty? @header = header # I. Prepare default package inclusions @default_packages = '' @default_packages_list.each do |package| next if @header && @header.match(/\{(?:#{package})\}/) # skip if overridden by the header. = @@default_package_options[package] setup_macro = nil # I.1. Expand common aliases, prepare extra setup steps case package when 'ams' # alias for a family of packages package = 'amsfonts,amsmath,amssymb' when 'hyperref' setup_macro = "\\hypersetup{colorlinks=false,pdfborder={0 0 0}}" when 'latexml' package = nil setup_macro = "\% You can conditionalize code for latexml or normal latex using this.\n"+ "\\newif\\iflatexml\\latexmlfalse" when 'babel' # handle globally, as we need to automagically internationalize any Unicode package = nil end # I.2. Add the package inclusion, if any if package @default_packages << if "\\usepackage[#{.join(',')}]{#{package}}" else "\\usepackage{#{package}}" end @default_packages << "\n" end # I.3 Add the setup macro, if any if setup_macro @default_packages << setup_macro + "\n" end end # II. Special graceful degradation treatment for common sources of conflicts, done once globally if !@style.package_compatible?(:natbib) @default_packages << "\n\\newcommand\\citet{\\cite}\n\\newcommand\\citep{\\cite}" end # III. Advanced auto-magical internationalization of unicode with babel (intended for use with pdflatex) if @style.package_compatible?(:babel) # Having the full body and preamble, figure out which flavours of babel we need (and potentially other text-dependent logic) ["default_packages"] = @default_packages ["header"] = @header preamble = @style.() # We'll have to rerender the preamble with all language locales setup @default_packages << UnicodeBabel::latex_inclusions(preamble + body) @default_packages << "\n" # And auto-deposit various language activation macros in the article itself body = UnicodeBabel::activate_foreign_languages(body) end # IV. Render the preamble and prepare the final latex document ["default_packages"] = @default_packages ["header"] = @header preamble = @style.() article = preamble + "\n\n" + body # IV.1. Normalize to simpler latex article = self.simplify_latex(article) # IV.2 Perform citations styling article = self.stylize_citations(article, ) # IV.3. Wrap up article << "\n\\end{document}" if @style.package_compatible?(:latex) # finalize latex documents article << "\n\n" return article end |
#simplify_latex(text) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/texstylist.rb', line 96 def simplify_latex(text) # \amp can be written as simply \& text = text.gsub(/\\amp([^\w])/, "\\\\&\\1") # simplify new line markup if needed text = text.gsub(/\r\n/, "\n") end |
#stylize_citations(article, metadata) ⇒ Object
103 104 105 |
# File 'lib/texstylist.rb', line 103 def stylize_citations(article, ) Citations::stylize_citations(article, ['bibliography'], @style, ['citation_style'], decorate: ['decorate_citations']) end |