Class: IsoDoc::Convert
- Defined in:
- lib/isodoc/css.rb,
lib/isodoc/convert.rb
Direct Known Subclasses
HeadlessHtmlConvert, HtmlConvert, PdfConvert, PresentationXMLConvert, WordConvert, XslfoPdfConvert
Constant Summary
Constants included from Function::Utils
Function::Utils::CLAUSE_ANCESTOR, Function::Utils::DOCTYPE_HDR, Function::Utils::NOKOHEAD, Function::Utils::NOTE_CONTAINER_ANCESTOR
Constants included from Function::Table
Constants included from Function::Section
Function::Section::TERM_CLAUSE
Constants included from Function::Lists
Constants included from Function::Cleanup
Function::Cleanup::FIGURE_WITH_FOOTNOTES
Constants included from Function::Blocks
Function::Blocks::EXAMPLE_TBL_ATTR, Function::Blocks::EXAMPLE_TD_ATTR
Instance Attribute Summary collapse
-
#i18n ⇒ Object
Returns the value of attribute i18n.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #convert(input_filename, file = nil, debug = false, output_filename = nil) ⇒ Object
- #convert1(docxml, filename, dir) ⇒ Object
- #convert_init(file, input_filename, debug) ⇒ Object
- #convert_scss(filename, stylesheet, stripwordcss) ⇒ Object
-
#default_file_locations(_options) ⇒ Object
none for this parent gem, but will be populated in child gems which have access to stylesheets &c.
- #default_fonts(_options) ⇒ Object
- #fonts_options ⇒ Object
-
#generate_css(filename, stripwordcss) ⇒ Object
stripwordcss if HTML stylesheet, !stripwordcss if DOC stylesheet.
- #html_doc_path(*file) ⇒ Object
- #i18n_init(lang, script, i18nyaml = nil) ⇒ Object
-
#initialize(options) ⇒ Convert
constructor
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 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_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.
- #l10n(x, lang = @lang, script = @script) ⇒ Object
- #metadata_init(lang, script, i18n) ⇒ Object
- #middle_clause(docxml = nil) ⇒ Object
-
#populate_css ⇒ Object
run this after @meta is populated.
-
#precompiled_style_or_original(stylesheet_path) ⇒ Object
Check if already compiled version(.css) exists, if not, return original scss file.
- #scss_fontheader(is_html_css) ⇒ Object
- #tmpimagedir_suffix ⇒ Object
- #xref_init(lang, script, _klass, i18n, options) ⇒ Object
Methods included from ClassUtils
Methods included from Function::Utils
#attr_code, #date_range, #empty2nil, #extract_delims, #from_xhtml, #get_clause_id, #get_note_container_id, #header_strip, #image_localfile, #insert_tab, #labelled_ancestor, #liquid, #noko, #ns, #populate_template, #save_dataimage, #sentence_join, #to_xhtml, #to_xhtml_fragment
Methods included from Function::ToWordHtml
#body_attr, #boilerplate, #define_head, #in_sourcecode, #info, #init_file, #make_body, #make_body1, #make_body2, #make_body3, #middle, #middle_admonitions, #middle_title, #note?, #parse, #rel_tmpimagedir, #set_termdomain, #tmpimagedir
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_delim, #termnote_parse, #termref_parse
Methods included from Function::Table
#colgroup, #make_tr_attr, #table_attrs, #table_parse, #table_title_parse, #tbody_parse, #tcaption, #tfoot_parse, #thead_parse, #tr_parse
Methods included from Function::Section
#abstract, #acknowledgements, #annex, #annex_attrs, #annex_name, #clause, #clause_attrs, #clause_name, #clause_parse, #clause_parse_title, #clausedelim, #clausedelimspace, #copyright_parse, #feedback_parse, #foreword, #inline_header_title, #introduction, #is_clause?, #legal_parse, #license_parse, #preface, #preface_block, #scope, #symbols_abbrevs, #symbols_parse, #terms_defs, #terms_parse
Methods included from Function::References
#bibitem_ref_code, #biblio_list, #bibliography, #bibliography_parse, #bibliography_xpath, #bracket_if_num, #date_note_process, #docid_l10n, #docid_prefix, #format_ref, #implicit_reference, #is_standard, #iso_bibitem_entry_attrs, #iso_title, #nonstd_bibitem, #norm_ref, #norm_ref_xpath, #omit_docid_prefix, #pref_ref_code, #prefix_bracketed_ref, #ref_entry_code, #reference_format, #render_identifier, #std_bibitem_entry
Methods included from Function::Lists
#dl_attrs, #dl_parse, #dt_dd?, #dt_parse, #li_parse, #ol_attrs, #ol_depth, #ol_parse, #ol_style, #ul_attrs, #ul_parse
Methods included from Function::Inline
#bookmark_parse, #br_parse, #callout_parse, #em_parse, #eref_parse, #eref_target, #error_parse, #hr_parse, #image_parse, #image_title_parse, #index_parse, #index_xref_parse, #keyword_parse, #link_parse, #no_locality_parse, #origin_parse, #page_break, #pagebreak_parse, #section_break, #smallcap_parse, #stem_parse, #strike_parse, #strong_parse, #sub_parse, #suffix_url, #sup_parse, #termrefelem_parse, #text_parse, #tt_parse, #underline_parse, #xref_parse
Methods included from Function::Cleanup
#admonition_cleanup, #break_up_long_strings, #break_up_long_strings1, #cleanup, #example_cleanup, #figure_aside_process, #figure_cleanup, #figure_get_or_make_dl, #footnote_cleanup, #footnote_reference_format, #inline_header_cleanup, #merge_fnref_into_fn_text, #new_fullcolspan_row, #passthrough_cleanup, #remove_bottom_border, #symbols_cleanup, #table_cleanup, #table_footnote_cleanup, #table_footnote_reference_format, #table_get_or_make_tfoot, #table_long_strings_cleanup, #table_note_cleanup, #termref_cleanup, #textcleanup
Methods included from Function::Blocks
#admonition_attrs, #admonition_class, #admonition_name, #admonition_name_parse, #admonition_parse, #annotation_parse, #example_div_attr, #example_div_parse, #example_label, #example_parse, #example_table_attr, #example_table_parse, #figure_attrs, #figure_key, #figure_name_parse, #figure_parse, #formula_attrs, #formula_parse, #formula_parse1, #formula_where, #keep_style, #note_attrs, #note_delim, #note_p_parse, #note_parse, #note_parse1, #para_attrs, #para_class, #para_parse, #passthrough_parse, #permission_parse, #pre_parse, #pseudocode_attrs, #pseudocode_parse, #quote_attribution, #quote_parse, #recommendation_attr_keyvalue, #recommendation_attr_parse, #recommendation_attributes, #recommendation_attributes1, #recommendation_labels, #recommendation_name, #recommendation_parse, #reqt_attrs, #reqt_component_attrs, #reqt_metadata_node, #requirement_component_parse, #requirement_parse, #requirement_skip_parse, #sourcecode_attrs, #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 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_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
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 |
# File 'lib/isodoc/convert.rb', line 37 def initialize() @libdir ||= File.dirname(__FILE__) .merge!(default_fonts()) do |_, old, new| old || new end .merge!(default_file_locations()) do |_, old, new| old || new end @options = @files_to_delete = [] @tempfile_cache = [] @htmlstylesheet_name = [:htmlstylesheet] @wordstylesheet_name = [:wordstylesheet] @standardstylesheet_name = [:standardstylesheet] @header = [:header] @htmlcoverpage = [:htmlcoverpage] @wordcoverpage = [:wordcoverpage] @htmlintropage = [:htmlintropage] @wordintropage = [:wordintropage] @normalfontsize = [:normalfontsize] @smallerfontsize = [:smallerfontsize] @monospacefontsize = [:monospacefontsize] @footnotefontsize = [:footnotefontsize] @scripts = [:scripts] @scripts_pdf = [:scripts_pdf] @i18nyaml = [:i18nyaml] @ulstyle = [:ulstyle] @olstyle = [:olstyle] @datauriimage = [:datauriimage] @suppressheadingnumbers = [:suppressheadingnumbers] @break_up_urls_in_tables = [:break_up_urls_in_tables] == 'true' @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 = [:language] || "en" @script = [:script] || "Latn" @maxwidth = 1200 @maxheight = 800 @wordToClevels = [:doctoclevels].to_i @wordToClevels = 2 if @wordToClevels.zero? @htmlToClevels = [:htmltoclevels].to_i @htmlToClevels = 2 if @htmlToClevels.zero? @bookmarks_allocated = { 'X' => true } @fn_bookmarks = {} end |
Instance Attribute Details
#i18n ⇒ Object
Returns the value of attribute i18n.
11 12 13 |
# File 'lib/isodoc/convert.rb', line 11 def i18n @i18n end |
#options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/isodoc/convert.rb', line 10 def @options end |
Instance Method Details
#convert(input_filename, file = nil, debug = false, output_filename = nil) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/isodoc/convert.rb', line 148 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) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/isodoc/convert.rb', line 106 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) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/isodoc/convert.rb', line 136 def convert_init(file, input_filename, debug) docxml = Nokogiri::XML(file) 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) (@lang, @script, @i18n) xref_init(@lang, @script, self, @i18n, {}) [docxml, filename, dir] end |
#convert_scss(filename, stylesheet, stripwordcss) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/isodoc/css.rb', line 63 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) ⇒ Object
none for this parent gem, but will be populated in child gems which have access to stylesheets &c
34 35 36 |
# File 'lib/isodoc/css.rb', line 34 def default_file_locations() {} end |
#default_fonts(_options) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/isodoc/css.rb', line 24 def default_fonts() { bodyfont: 'Arial', headerfont: 'Arial', monospacefont: 'Courier', } end |
#fonts_options ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/isodoc/css.rb', line 38 def { 'bodyfont' => [:bodyfont] || 'Arial', 'headerfont' => [:headerfont] || 'Arial', 'monospacefont' => [:monospacefont] || 'Courier', "normalfontsize" => [:normalfontsize], "monospacefontsize" => [:monospacefontsize], "smallerfontsize" => [:smallerfontsize], "footnotefontsize" => [:footnotefontsize] } end |
#generate_css(filename, stripwordcss) ⇒ Object
stripwordcss if HTML stylesheet, !stripwordcss if DOC stylesheet
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/isodoc/css.rb', line 78 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) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/isodoc/convert.rb', line 98 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) ⇒ Object
128 129 130 |
# File 'lib/isodoc/convert.rb', line 128 def i18n_init(lang, script, i18nyaml = nil) @i18n = I18n.new(lang, script, i18nyaml || @i18nyaml) end |
#l10n(x, lang = @lang, script = @script) ⇒ Object
132 133 134 |
# File 'lib/isodoc/convert.rb', line 132 def l10n(x, lang = @lang, script = @script) @i18n.l10n(x, lang, script) end |
#metadata_init(lang, script, i18n) ⇒ Object
119 120 121 |
# File 'lib/isodoc/convert.rb', line 119 def (lang, script, i18n) @meta = Metadata.new(lang, script, i18n) end |
#middle_clause(docxml = nil) ⇒ Object
160 161 162 163 |
# File 'lib/isodoc/convert.rb', line 160 def middle_clause(docxml = nil) "//clause[parent::sections][not(@type = 'scope')]"\ '[not(descendant::terms)]' end |
#populate_css ⇒ Object
run this after @meta is populated
18 19 20 21 22 |
# File 'lib/isodoc/css.rb', line 18 def populate_css @htmlstylesheet = generate_css(@htmlstylesheet_name, true) @wordstylesheet = generate_css(@wordstylesheet_name, false) @standardstylesheet = generate_css(@standardstylesheet_name, false) end |
#precompiled_style_or_original(stylesheet_path) ⇒ Object
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
6 7 8 9 10 11 12 13 14 15 |
# 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) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/isodoc/css.rb', line 50 def scss_fontheader(is_html_css) b = [:bodyfont] || 'Arial' h = [:headerfont] || 'Arial' m = [:monospacefont] || 'Courier' ns = [:normalfontsize] || (is_html_css ? "1.0em" : '12.0pt') ms = [:monospacefontsize] || (is_html_css ? "0.8em" : '11.0pt') ss = [:smallerfontsize] || (is_html_css ? "0.9em" : '10.0pt') fs = [: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 |
#tmpimagedir_suffix ⇒ Object
94 95 96 |
# File 'lib/isodoc/convert.rb', line 94 def tmpimagedir_suffix '_images' end |
#xref_init(lang, script, _klass, i18n, options) ⇒ Object
123 124 125 126 |
# File 'lib/isodoc/convert.rb', line 123 def xref_init(lang, script, _klass, i18n, ) html = HtmlConvert.new(language: @lang, script: @script) @xrefs = Xref.new(lang, script, html, i18n, ) end |