class IsoDoc::Convert

Attributes

i18n[RW]
meta[RW]
options[RW]
xrefs[RW]

Public Class Methods

new(options) click to toggle source

htmlstylesheet: Generic stylesheet for HTML htmlstylesheet_override: Override stylesheet for HTML wordstylesheet: Generic stylesheet for Word wordstylesheet_override: Override stylesheet for Word standardsheet: Stylesheet specific to Standard header: Header file for Word htmlcoverpage: Cover page for HTML wordcoverpage: Cover page for Word htmlintropage: Introductory page for HTML wordintropage: Introductory page for Word normalfontsize: Font size for body text smallerfontsize: Font size for smaller than body text monospacefontsize: Font size for monospace font footnotefontsize: Font size for footnotes i18nyaml: YAML file for internationalisation of text ulstyle: list style in Word CSS for unordered lists olstyle: list style in Word CSS for ordered lists bodyfont: font to use for body text headerfont: font to use for header text monospace: font to use for monospace text suppressheadingnumbers: suppress heading numbers for clauses scripts: Scripts file for HTML scripts_override: Override scripts file for HTML scripts_pdf: Scripts file for PDF (not used in XSLT PDF) datauriimage: Encode images in HTML output as data URIs break_up_urls_in_tables: whether to insert spaces in URLs in tables

every 40-odd chars

sectionsplit: split up HTML output on sections bare: do not insert any prefatory material (coverpage, boilerplate)

# File lib/isodoc/convert.rb, line 42
def initialize(options)
  @libdir ||= File.dirname(__FILE__) # rubocop:disable Lint/DisjunctiveAssignmentInConstructor
  options.merge!(default_fonts(options)) do |_, old, new|
    old || new
  end.merge!(default_file_locations(options)) do |_, old, new|
    old || new
  end
  @options = options
  @files_to_delete = []
  @tempfile_cache = []
  @htmlstylesheet_name = options[:htmlstylesheet]
  @wordstylesheet_name = options[:wordstylesheet]
  @htmlstylesheet_override_name = options[:htmlstylesheet_override]
  @wordstylesheet_override_name = options[:wordstylesheet_override]
  @standardstylesheet_name = options[:standardstylesheet]
  @sourcefilename = options[:sourcefilename]
  @header = options[:header]
  @htmlcoverpage = options[:htmlcoverpage]
  @wordcoverpage = options[:wordcoverpage]
  @htmlintropage = options[:htmlintropage]
  @wordintropage = options[:wordintropage]
  @normalfontsize = options[:normalfontsize]
  @smallerfontsize = options[:smallerfontsize]
  @monospacefontsize = options[:monospacefontsize]
  @footnotefontsize = options[:footnotefontsize]
  @scripts = options[:scripts] ||
    File.join(File.dirname(__FILE__), "base_style", "scripts.html")
  @scripts_pdf = options[:scripts_pdf]
  @scripts_override = options[:scripts_override]
  @i18nyaml = options[:i18nyaml]
  @ulstyle = options[:ulstyle]
  @olstyle = options[:olstyle]
  @datauriimage = options[:datauriimage]
  @suppressheadingnumbers = options[:suppressheadingnumbers]
  @break_up_urls_in_tables = options[:break_up_urls_in_tables] == "true"
  @sectionsplit = options[:sectionsplit] == "true"
  @suppressasciimathdup = options[:suppressasciimathdup] == "true"
  @bare = options[:bare]
  @termdomain = ""
  @termexample = false
  @note = false
  @sourcecode = false
  @footnotes = []
  @comments = []
  @in_footnote = false
  @in_comment = false
  @in_table = false
  @in_figure = false
  @seen_footnote = Set.new
  @c = HTMLEntities.new
  @openmathdelim = "`"
  @closemathdelim = "`"
  @lang = options[:language] || "en"
  @script = options[:script] || "Latn"
  @maxwidth = 1200
  @maxheight = 800
  @wordToClevels = options[:doctoclevels].to_i
  @wordToClevels = 2 if @wordToClevels.zero?
  @htmlToClevels = options[:htmltoclevels].to_i
  @htmlToClevels = 2 if @htmlToClevels.zero?
  @bookmarks_allocated = { "X" => true }
  @fn_bookmarks = {}
end

Public Instance Methods

convert(input_filename, file = nil, debug = false, output_filename = nil) click to toggle source
# File lib/isodoc/convert.rb, line 160
def convert(input_filename, file = nil, debug = false,
            output_filename = nil)
  file = File.read(input_filename, encoding: "utf-8") if file.nil?
  @openmathdelim, @closemathdelim = extract_delims(file)
  docxml, filename, dir = convert_init(file, input_filename, debug)
  result = convert1(docxml, filename, dir)
  return result if debug

  output_filename ||= "#{filename}.#{@suffix}"
  postprocess(result, output_filename, dir)
  FileUtils.rm_rf dir
end
convert1(docxml, filename, dir) click to toggle source
# File lib/isodoc/convert.rb, line 118
def convert1(docxml, filename, dir)
  @xrefs.parse docxml
  noko do |xml|
    xml.html **{ lang: @lang.to_s } do |html|
      html.parent.add_namespace("epub", "http://www.idpf.org/2007/ops")
      info docxml, nil
      populate_css
      html.head { |head| define_head head, filename, dir }
      make_body(html, docxml)
    end
  end.join("\n")
end
convert_init(file, input_filename, debug) click to toggle source
# File lib/isodoc/convert.rb, line 148
def convert_init(file, input_filename, debug)
  docxml = Nokogiri::XML(file) { |config| config.huge }
  filename, dir = init_file(input_filename, debug)
  docxml.root.default_namespace = ""
  lang = docxml&.at(ns("//bibdata/language"))&.text and @lang = lang
  script = docxml&.at(ns("//bibdata/script"))&.text and @script = script
  i18n_init(@lang, @script)
  metadata_init(@lang, @script, @i18n)
  xref_init(@lang, @script, self, @i18n, {})
  [docxml, filename, dir]
end
convert_scss(filename, stylesheet, stripwordcss) click to toggle source
# File lib/isodoc/css.rb, line 77
def convert_scss(filename, stylesheet, stripwordcss)
  require "sassc"
  require "isodoc/sassc_importer"

  [File.join(Gem.loaded_specs["isodoc"].full_gem_path,
             "lib", "isodoc"),
             File.dirname(filename)].each do |name|
               SassC.load_paths << name
             end
             SassC::Engine.new(scss_fontheader(stripwordcss) + stylesheet,
                               syntax: :scss, importer: SasscImporter)
               .render
end
default_file_locations(_options) click to toggle source

none for this parent gem, but will be populated in child gems which have access to stylesheets &c

# File lib/isodoc/css.rb, line 48
def default_file_locations(_options)
  {}
end
default_fonts(_options) click to toggle source
# File lib/isodoc/css.rb, line 38
def default_fonts(_options)
  {
    bodyfont: "Arial",
    headerfont: "Arial",
    monospacefont: "Courier New",
  }
end
fonts_options() click to toggle source
# File lib/isodoc/css.rb, line 52
def fonts_options
  {
    "bodyfont" => options[:bodyfont] || "Arial",
    "headerfont" => options[:headerfont] || "Arial",
    "monospacefont" => options[:monospacefont] || "Courier New",
    "normalfontsize" => options[:normalfontsize],
    "monospacefontsize" => options[:monospacefontsize],
    "smallerfontsize" => options[:smallerfontsize],
    "footnotefontsize" => options[:footnotefontsize],
  }
end
generate_css(filename, stripwordcss) click to toggle source

stripwordcss if HTML stylesheet, !stripwordcss if DOC stylesheet

# File lib/isodoc/css.rb, line 92
def generate_css(filename, stripwordcss)
  return nil if filename.nil?

  filename = precompiled_style_or_original(filename)
  stylesheet = File.read(filename, encoding: "UTF-8")
  stylesheet = populate_template(stylesheet, :word)
  stylesheet.gsub!(/(\s|\{)mso-[^:]+:[^;]+;/m, "\\1") if stripwordcss
  if File.extname(filename) == ".scss"
    stylesheet = convert_scss(filename, stylesheet, stripwordcss)
  end
  Tempfile.open([File.basename(filename, ".*"), "css"],
                encoding: "utf-8") do |f|
    f.write(stylesheet)
    f
  end
end
html_doc_path(*file) click to toggle source
# File lib/isodoc/convert.rb, line 110
def html_doc_path(*file)
  file.each do |f|
    ret = File.join(@libdir, File.join("html", f))
    File.exist?(ret) and return ret
  end
  nil
end
i18n_init(lang, script, i18nyaml = nil) click to toggle source
# File lib/isodoc/convert.rb, line 140
def i18n_init(lang, script, i18nyaml = nil)
  @i18n = I18n.new(lang, script, i18nyaml || @i18nyaml)
end
l10n(expr, lang = @lang, script = @script) click to toggle source
# File lib/isodoc/convert.rb, line 144
def l10n(expr, lang = @lang, script = @script)
  @i18n.l10n(expr, lang, script)
end
localpath(path) click to toggle source
# File lib/isodoc/css.rb, line 19
def localpath(path)
  return path if %r{^[A-Z]:|^/|^file:/}.match?(path)
  return path unless (@sourcedir || @localdir) && path

  File.expand_path(File.join((@sourcedir || @localdir), path))
end
metadata_init(lang, script, i18n) click to toggle source
# File lib/isodoc/convert.rb, line 131
def metadata_init(lang, script, i18n)
  @meta = Metadata.new(lang, script, i18n)
end
middle_clause(_docxml = nil) click to toggle source
# File lib/isodoc/convert.rb, line 173
def middle_clause(_docxml = nil)
  "//clause[parent::sections][not(@type = 'scope')]"\
    "[not(descendant::terms)]"
end
populate_css() click to toggle source

run this after @meta is populated

# File lib/isodoc/css.rb, line 27
def populate_css
  @htmlstylesheet = generate_css(localpath(@htmlstylesheet_name), true)
  @wordstylesheet = generate_css(localpath(@wordstylesheet_name), false)
  @standardstylesheet =
    generate_css(localpath(@standardstylesheet_name), false)
  @htmlstylesheet_override_name and @htmlstylesheet_override =
    File.open(localpath(@htmlstylesheet_override_name))
  @wordstylesheet_override_name and @wordstylesheet_override =
    File.open(localpath(@wordstylesheet_override_name))
end
precompiled_style_or_original(stylesheet_path) click to toggle source

Check if already compiled version(.css) exists,

if not, return original scss file. During release
we compile scss into css files in order to not depend on scss
# File lib/isodoc/css.rb, line 6
def precompiled_style_or_original(stylesheet_path)
  # Already have compiled stylesheet, use it
  return stylesheet_path if stylesheet_path.nil? ||
    File.extname(stylesheet_path) == ".css"

  basename = File.basename(stylesheet_path, ".*")
  compiled_path = File.join(File.dirname(stylesheet_path),
                            "#{basename}.css")
  return stylesheet_path unless File.file?(compiled_path)

  compiled_path
end
scss_fontheader(is_html_css) click to toggle source
# File lib/isodoc/css.rb, line 64
def scss_fontheader(is_html_css)
  b = options[:bodyfont] || "Arial"
  h = options[:headerfont] || "Arial"
  m = options[:monospacefont] || "Courier New"
  ns = options[:normalfontsize] || (is_html_css ? "1.0em" : "12.0pt")
  ms = options[:monospacefontsize] || (is_html_css ? "0.8em" : "11.0pt")
  ss = options[:smallerfontsize] || (is_html_css ? "0.9em" : "10.0pt")
  fs = options[:footnotefontsize] || (is_html_css ? "0.9em" : "9.0pt")
  "$bodyfont: #{b};\n$headerfont: #{h};\n$monospacefont: #{m};\n"\
    "$normalfontsize: #{ns};\n$monospacefontsize: #{ms};\n"\
    "$smallerfontsize: #{ss};\n$footnotefontsize: #{fs};\n"
end
target_pdf(node) click to toggle source
# File lib/isodoc/convert.rb, line 178
def target_pdf(node)
  if /#/.match?(node["target"])
    node["target"].sub(/#/, ".pdf#")
  else
    "##{node['target']}"
  end
end
tmpimagedir_suffix() click to toggle source
# File lib/isodoc/convert.rb, line 106
def tmpimagedir_suffix
  "_images"
end
xref_init(lang, script, _klass, i18n, options) click to toggle source
# File lib/isodoc/convert.rb, line 135
def xref_init(lang, script, _klass, i18n, options)
  html = HtmlConvert.new(language: @lang, script: @script)
  @xrefs = Xref.new(lang, script, html, i18n, options)
end