From 9b15e307f4036a19f6e40fc2672ddc90ed1d5812 Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Mon, 9 Apr 2018 21:19:50 +0000 Subject: tex4ebook (9apr18) git-svn-id: svn://tug.org/texlive/trunk@47414 c570f23f-e606-0410-a88d-b1316a301751 --- Master/texmf-dist/scripts/tex4ebook/exec_epub.lua | 4 +- Master/texmf-dist/scripts/tex4ebook/exec_epub3.lua | 68 ++++++++++++++++++++++ Master/texmf-dist/scripts/tex4ebook/tex4ebook | 37 ++++++++++-- 3 files changed, 103 insertions(+), 6 deletions(-) (limited to 'Master/texmf-dist/scripts') diff --git a/Master/texmf-dist/scripts/tex4ebook/exec_epub.lua b/Master/texmf-dist/scripts/tex4ebook/exec_epub.lua index 1311516bfb6..d556c10ef55 100755 --- a/Master/texmf-dist/scripts/tex4ebook/exec_epub.lua +++ b/Master/texmf-dist/scripts/tex4ebook/exec_epub.lua @@ -119,7 +119,9 @@ local mimetypes = { otf = "application/opentype", ttf = "application/truetype", woff = "application/font-woff", - js = "text/javascript" + js = "text/javascript", + mp3 = "audio/mpeg", + smil = "application/smil+xml" } function make_opf() diff --git a/Master/texmf-dist/scripts/tex4ebook/exec_epub3.lua b/Master/texmf-dist/scripts/tex4ebook/exec_epub3.lua index eb829284b59..56eff3f341c 100755 --- a/Master/texmf-dist/scripts/tex4ebook/exec_epub3.lua +++ b/Master/texmf-dist/scripts/tex4ebook/exec_epub3.lua @@ -1,5 +1,6 @@ module(...,package.seeall) local eb = require("exec_epub") +local dom = require("luaxml-domobject") local ext = "xhtml" local outputdir = nil @@ -42,6 +43,72 @@ local function makeTOC(document) return template end +local function add_media_overlays(content) + local add_meta = function(package, attributes, text) + local meta = package:create_element("meta",attributes) + local dur_el = meta:create_text_node(text) + meta:add_child_node(dur_el) + package:add_child_node(meta) + end + -- calculate total audio time + local calc_times = function(times) + local time = 0 + for _, curr in ipairs(times) do + -- smil file contains timestamps in the H:M:S format, we need to parse it + local hours, minutes, seconds = curr:match("(%d+):(%d+):(%d+)") + time = time + os.time({year=1970, day=1, month=1, hour=hours, min=minutes, sec=seconds}) + end + return os.date("%H:%M:%S",time) + end + local opfdom = dom.parse(content) + local items = opfdom:query_selector("manifest item") + local ref = {} + local times = {} + local package = opfdom:query_selector("metadata")[1] + -- we must read all smil files and find references to html files + -- it is necessary to add media-overlay attribute to the referenced items + for _, item in ipairs(items) do + local href = item:get_attribute("href") + ref[href] = item + -- we must read audio length from the smil file and add it as a property + if href:match("smil$") then + local f = io.open(outputdir .. "/" .. href, "r") + if not f then break end + local smil = f:read("*all") + f:close() + local smildom = dom.parse(smil) + local audios = smildom:query_selector("audio") + local last = audios[#audios] + -- add audio duration to the metadata section + if last then + local duration = last:get_attribute("clipend") + if duration then + -- todo: calculate total audio length + table.insert(times, duration) + local audio_id = item:get_attribute("id") + add_meta(package, {property="media:duration", refines="#"..audio_id}, duration) + end + end + + -- add the media-overlay attribute + local textref = smil:match('epub:textref="(.-)"') + local id = item:get_attribute("id") + local referenced = ref[textref] + if referenced then + referenced:set_attribute("media-overlay", id) + end + end + end + -- calculate length of all media overlay audio files + if #times > 0 then + local totaltime = calc_times(times) + add_meta(package,{property="media:duration"}, totaltime) + end + local serialized = opfdom:serialize() + return serialized +end + + local function cleanOPF() -- in epub3, there must be table of contents -- if there is no toc in the document, we must add generic one @@ -72,6 +139,7 @@ local function cleanOPF() -- remove empty guide element end content = content:gsub("%s*","") + content = add_media_overlays(content) f = io.open(outputdir .. "/" ..opf,"w") f:write(content) f:close() diff --git a/Master/texmf-dist/scripts/tex4ebook/tex4ebook b/Master/texmf-dist/scripts/tex4ebook/tex4ebook index 5c9f65573f7..02906427453 100755 --- a/Master/texmf-dist/scripts/tex4ebook/tex4ebook +++ b/Master/texmf-dist/scripts/tex4ebook/tex4ebook @@ -7,6 +7,7 @@ require("lapp-mk4") -- require("ebookutils") local ebookutils = require "mkutils" local mkparams = require "mkparams" +local mk_config = require "make4ht-config" -- Setting local latex_cmd="latex" @@ -37,6 +38,7 @@ tex4ebook [switches] filename ["tex4ht.sty op." "tex4ht op." "t4ht op" "latex op -s,--shell-escape Enable shell escape in htlatex run -t,--tidy Run html tidy on html output. May result in wrong spacing! -v,--version Display version number +-x,--xetex Use xelatex for document compilation (string) Input file name ]] @@ -52,12 +54,14 @@ else end if args.version then - print "tex4ebook v0.1e" + print "tex4ebook v0.2" return end if args.lua then latex_cmd="dvilualatex" +elseif args.xetex then + latex_cmd="xelatex --no-pdf" end @@ -125,12 +129,17 @@ local latex_par = latex_par + args[5] args.filename=input_file local params = mkparams.process_args(args) + +local output_format = params.output_format --args.format +-- use epub as default output_format +output_format = output_format or "epub" +local extensions = ebookutils.load_extensions(params.extensions, output_format) -- but also support tex4ebook!s own parameters local oldparams = { -- htlatex=latex_cmd -- ,input=input -- ,tex_file = tex_file - format=args.format + format= output_format -- ,latex_par=latex_par -- ,tex4ht_sty_par=tex4ht_sty_par -- ,tex4ht_par=tex4ht_par @@ -152,14 +161,28 @@ end params.tex4ht_sty_par=params.tex4ht_sty_par .. ",charset=utf-8,"..args.format params.tex4ht_par= " -cmozhtf -utf8" .. params.tex4ht_par -if output_formats[args.format] then - executor=require("exec_"..args.format) + +if output_formats[output_format] then + executor=require("exec_"..output_format) params=executor.prepare(params) + if #extensions > 0 then + params = ebookutils.extensions_prepare_parameters(extensions,params) + end else - print("Unknown output format: "..args.format) + print("Unknown output format: "..output_format) return end +-- find tex4ebook configuration file +local configname = "tex4ebook" +local conffile = mk_config.find_config(configname) or mk_config.find_xdg_config(configname) +if conffile then + print("Using configuration file: " .. conffile) + ebookutils.load_config(params, conffile) +else + print "No conffile" +end + local build_file = input.. ".mk4" if args["build-file"] and args["build-file"] ~= "nil" then @@ -171,6 +194,10 @@ local config_file = ebookutils.load_config(params, build_file) params["config_file"] = config_file --config_file.Make:run() print("${htlatex} ${input} \"${tex4ht_sty_par}\" \"${tex4ht_par}\" \"${t4ht_par}\" \"${latex_par}\"" % params) +if #extensions > 0 then + config_file.Make = ebookutils.extensions_modify_build(extensions, config_file.Make) +end + executor.run(input,params) executor.writeContainer() executor.clean() -- cgit v1.2.3