Class: IsoDoc::Convert

Inherits:
Common
  • Object
show all
Defined in:
lib/isodoc/convert.rb

Constant Summary

Constants included from Function::XrefGen

Function::XrefGen::CHILD_EXAMPLES_XPATH, Function::XrefGen::CHILD_NOTES_XPATH, Function::XrefGen::CHILD_SECTIONS, Function::XrefGen::FIRST_LVL_REQ, Function::XrefGen::SECTIONS_XPATH

Constants included from Function::Utils

Function::Utils::CLAUSE_ANCESTOR, Function::Utils::NOKOHEAD, Function::Utils::NOTE_CONTAINER_ANCESTOR

Constants included from Function::Table

Function::Table::SW

Constants included from Function::Section

Function::Section::MIDDLE_CLAUSE, Function::Section::TERM_CLAUSE

Constants included from Function::References

Function::References::BIBLIOGRAPHY_XPATH, Function::References::ISO_PUBLISHER_XPATH

Constants included from Function::Lists

Function::Lists::OL_STYLE

Constants included from Function::Cleanup

Function::Cleanup::FIGURE_WITH_FOOTNOTES

Constants included from Function::Blocks

Function::Blocks::EXAMPLE_TBL_ATTR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassUtils

#date_range, #liquid, #ns

Methods included from Function::XrefSectGen

#annex_name_lbl, #annex_names, #annex_names1, #back_anchor_names, #clause_names, #initial_anchor_names, #middle_section_asset_names, #preface_clause_name, #preface_names, #preface_names1, #section_names, #section_names1

Methods included from Function::XrefGen

#anchor, #anchor_names, #anchor_struct, #anchor_struct_label, #anchor_struct_xref, #example_anchor_names, #get_anchors, #hierarchical_asset_names, #hierarchical_figure_names, #hierarchical_formula_names, #hierarchical_permission_names, #hierarchical_recommendation_names, #hierarchical_requirement_names, #hierarchical_table_names, #hierfigsep, #hiersep, #list_anchor_names, #list_item_anchor_names, #listlabel, #note_anchor_names, #sequential_asset_names, #sequential_figure_names, #sequential_formula_names, #sequential_permission_names, #sequential_permission_names1, #sequential_recommendation_names, #sequential_recommendation_names1, #sequential_requirement_names, #sequential_requirement_names1, #sequential_table_names, #termexample_anchor_names, #termnote_anchor_names, #termnote_label

Methods included from Function::Utils

#attr_code, #date_range, #empty2nil, #extract_delims, #from_xhtml, #get_clause_id, #get_note_container_id, #header_strip, #insert_tab, #liquid, #noko, #ns, #populate_template, #save_dataimage, #sentence_join, #to_xhtml, #to_xhtml_fragment

Methods included from Function::ToWordHtml

#bookmark_parse, #define_head, #em_parse, #in_sourcecode, #info, #init_file, #keyword_parse, #make_body, #make_body1, #make_body2, #make_body3, #middle, #middle_title, #note?, #parse, #rel_tmpimagedir, #set_termdomain, #smallcap_parse, #strike_parse, #strong_parse, #sub_parse, #sup_parse, #text_parse, #tmpimagedir, #tt_parse

Methods included from Function::Terms

#admitted_term_parse, #definition_parse, #deprecated_term_parse, #modification_parse, #para_then_remainder, #term_parse, #termdef_parse, #termdocsource_parse, #termnote_parse, #termref_parse

Methods included from Function::Table

#make_table_attr, #make_tr_attr, #table_parse, #table_title_parse, #tbody_parse, #tfoot_parse, #thead_parse, #tr_parse

Methods included from Function::Section

#abstract, #annex, #annex_name, #clause, #clause_name, #clause_parse, #clause_parse_title, #clausedelim, #external_terms_boilerplate, #foreword, #inline_header_title, #internal_external_terms_boilerplate, #introduction, #scope, #symbols_abbrevs, #symbols_parse, #term_defs_boilerplate, #term_defs_boilerplate_cont, #terms_defs, #terms_defs_title, #terms_parse

Methods included from Function::References

#biblio_list, #bibliography, #bibliography_parse, #date_note_process, #docid_l10n, #docid_prefix, #format_ref, #implicit_reference, #iso_bibitem_entry, #iso_bibitem_entry_attrs, #iso_bibitem_ref_code, #iso_title, #noniso_bibitem, #norm_ref, #norm_ref_preface, #omit_docid_prefix, #prefix_bracketed_ref, #ref_entry_code, #reference_format, #reference_names, #split_bibitems

Methods included from Function::Lists

#dl_parse, #dt_dd?, #dt_parse, #li_parse, #ol_depth, #ol_parse, #ol_style, #ul_parse

Methods included from Function::Inline

#anchor_linkend, #callout_parse, #eref_localities, #eref_parse, #error_parse, #get_linkend, #link_parse, #page_break, #prefix_container, #section_break, #stem_parse, #xref_parse

Methods included from Function::I18n

#eref_localities1, #eref_localities1_zh, #i18n_init, l10n, #load_yaml

Methods included from Function::Cleanup

#admonition_cleanup, #cleanup, #example_cleanup, #extract_symbols_list, #figure_aside_process, #figure_cleanup, #figure_get_or_make_dl, #footnote_cleanup, #inline_header_cleanup, #merge_fnref_into_fn_text, #new_fullcolspan_row, #remove_bottom_border, #symbol_key, #symbols_cleanup, #table_cleanup, #table_footnote_cleanup, #table_get_or_make_tfoot, #table_note_cleanup

Methods included from Function::Blocks

#admonition_class, #admonition_name, #admonition_name_parse, #admonition_parse, #annotation_parse, #example_div_parse, #example_label, #example_parse, #example_table_attr, #example_table_parse, #figure_key, #figure_name_parse, #figure_parse, #formula_parse, #formula_parse1, #formula_where, #image_parse, #image_title_parse, #note_label, #note_p_parse, #note_parse, #note_parse1, #para_attrs, #para_class, #para_parse, #permission_parse, #pre_parse, #quote_attribution, #quote_parse, #recommendation_name, #recommendation_parse, #requirement_component_parse, #requirement_parse, #requirement_skip_parse, #sourcecode_name_parse, #sourcecode_parse

Constructor Details

#initialize(options) ⇒ Convert

htmlstylesheet: Generic stylesheet for HTML wordstylesheet: Generic 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 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_pdf: Scripts file for PDF datauriimage: Encode images in HTML output as data URIs



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
# File 'lib/isodoc/convert.rb', line 28

def initialize(options)
  @libdir = File.dirname(__FILE__) unless @libdir
  options.merge!(default_fonts(options)) { |_, old, new| old || new }.
    merge!(default_file_locations(options)) { |_, old, new| old || new }
  @options = options
  @files_to_delete = []
  @htmlstylesheet_name = options[:htmlstylesheet]
  @wordstylesheet_name = options[:wordstylesheet]
  @standardstylesheet_name = options[:standardstylesheet]
  @header = options[:header]
  @htmlcoverpage = options[:htmlcoverpage]
  @wordcoverpage = options[:wordcoverpage]
  @htmlintropage = options[:htmlintropage]
  @wordintropage = options[:wordintropage]
  @scripts = options[:scripts]
  @scripts_pdf = options[:scripts_pdf]
  @i18nyaml = options[:i18nyaml]
  @ulstyle = options[:ulstyle]
  @olstyle = options[:olstyle]
  @datauriimage = options[:datauriimage]
  @suppressheadingnumbers = options[:suppressheadingnumbers]
  @termdomain = ""
  @termexample = false
  @note = false
  @sourcecode = false
  @anchors = {}
  @footnotes = []
  @comments = []
  @in_footnote = false
  @in_comment = false
  @in_table = false
  @in_figure = false
  @seen_footnote = Set.new
  @c = HTMLEntities.new
  @openmathdelim = "`"
  @closemathdelim = "`"
  @lang = "en"
  @script = "Latn"
  @maxwidth = 1200
  @maxheight = 800
  @wordToClevels = options[:doctoclevels].to_i
  @wordToClevels = 2 if @wordToClevels == 0
  @htmlToClevels = options[:htmltoclevels].to_i
  @htmlToClevels = 2 if @htmlToClevels == 0
end

Instance Attribute Details

#labels=(value) ⇒ Object (writeonly)

Sets the attribute labels

Parameters:

  • value

    the value to set the attribute labels to.



8
9
10
# File 'lib/isodoc/convert.rb', line 8

def labels=(value)
  @labels = value
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/isodoc/convert.rb', line 7

def options
  @options
end

Instance Method Details

#convert(filename, file = nil, debug = false) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/isodoc/convert.rb', line 163

def convert(filename, file = nil, debug = false)
  file = File.read(filename, encoding: "utf-8") if file.nil?
  @openmathdelim, @closemathdelim = extract_delims(file)
  docxml, filename, dir = convert_init(file, filename, debug)
  result = convert1(docxml, filename, dir)
  return result if debug
  postprocess(result, filename, dir)
  FileUtils.rm_rf dir
end

#convert1(docxml, filename, dir) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/isodoc/convert.rb', line 135

def convert1(docxml, filename, dir)
  anchor_names docxml
  noko do |xml|
    xml.html do |html|
      html.parent.add_namespace("epub", "http://www.idpf.org/2007/ops")
      info docxml, nil
      populate_css()
      define_head html, filename, dir
      make_body(html, docxml)
    end
  end.join("\n")
end

#convert_init(file, filename, debug) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/isodoc/convert.rb', line 152

def convert_init(file, filename, debug)
  docxml = Nokogiri::XML(file)
  filename, dir = init_file(filename, debug)
  docxml.root.default_namespace = ""
  lang = docxml&.at(ns("//bibdata/language"))&.text || @lang
  script = docxml&.at(ns("//bibdata/script"))&.text || @script
  i18n_init(lang, script)
  (lang, script, @labels)
  [docxml, filename, dir]
end

#default_file_locations(_options) ⇒ Object

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

htmlstylesheet: html_doc_path("htmlstyle.scss"),
htmlcoverpage: html_doc_path("html_rsd_titlepage.html"),
htmlintropage: html_doc_path("html_rsd_intro.html"),
scripts: html_doc_path("scripts.html"),
wordstylesheet: html_doc_path("wordstyle.scss"),
standardstylesheet: html_doc_path("rsd.scss"),
header: html_doc_path("header.html"),
wordcoverpage: html_doc_path("word_rsd_titlepage.html"),
wordintropage: html_doc_path("word_rsd_intro.html"),
ulstyle: l3
olstyle: l2



107
108
109
# File 'lib/isodoc/convert.rb', line 107

def default_file_locations(_options)
  {}
end

#default_fonts(_options) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/isodoc/convert.rb', line 85

def default_fonts(_options)
  {
    bodyfont: "Arial",
    headerfont: "Arial",
    monospacefont: "Courier",
  }
end

#extract_fonts(options) ⇒ Object

extract fonts for use in generate_css



112
113
114
115
116
117
# File 'lib/isodoc/convert.rb', line 112

def extract_fonts(options)
  b = options[:bodyfont] || "Arial"
  h = options[:headerfont] || "Arial"
  m = options[:monospacefont] || "Courier"
  "$bodyfont: #{b};\n$headerfont: #{h};\n$monospacefont: #{m};\n"
end

#generate_css(filename, stripwordcss, fontheader) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/isodoc/convert.rb', line 123

def generate_css(filename, stripwordcss, fontheader)
  return nil unless filename
  stylesheet = File.read(filename, encoding: "UTF-8")
  stylesheet = populate_template(stylesheet, :word)
  stylesheet.gsub!(/(\s|\{)mso-[^:]+:[^;]+;/m, "\\1") if stripwordcss
  engine = SassC::Engine.new(fontheader + stylesheet, syntax: :scss)
  outname = File.basename(filename, ".*") + ".css"
  File.open(outname, "w:UTF-8") { |f| f.write(engine.render) }
  @files_to_delete << outname
  outname
end

#html_doc_path(file) ⇒ Object



119
120
121
# File 'lib/isodoc/convert.rb', line 119

def html_doc_path(file)
  File.join(@libdir, File.join("html", file))
end

#metadata_init(lang, script, labels) ⇒ Object



148
149
150
# File 'lib/isodoc/convert.rb', line 148

def (lang, script, labels)
  @meta = .new(lang, script, labels)
end

#populate_cssObject

run this after @meta is populated



75
76
77
78
79
# File 'lib/isodoc/convert.rb', line 75

def populate_css
  @htmlstylesheet = generate_css(@htmlstylesheet_name, true, extract_fonts(options))
  @wordstylesheet = generate_css(@wordstylesheet_name, false, extract_fonts(options))
  @standardstylesheet = generate_css(@standardstylesheet_name, false, extract_fonts(options))
end

#tmpimagedir_suffixObject



81
82
83
# File 'lib/isodoc/convert.rb', line 81

def tmpimagedir_suffix
  "_images"
end