From 275c49dc5c88ef31c159297598c106480c6789ba Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Sun, 1 May 2016 14:01:29 -0400 Subject: [PATCH] nitcatalog: copy local image resources inside the output directory Signed-off-by: Jean Privat --- src/nitcatalog.nit | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/nitcatalog.nit b/src/nitcatalog.nit index 29477c1..b3dcd32 100644 --- a/src/nitcatalog.nit +++ b/src/nitcatalog.nit @@ -131,7 +131,68 @@ g.defer=true; g.async=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s); end end +redef class NitdocDecorator + redef fun add_image(v, link, name, comment) + do + # Keep absolute links as is + if link.has_prefix("http://") or link.has_prefix("https://") then + super + return + end + + do + # Get the directory of the doc object to deal with the relative link + var mdoc = current_mdoc + if mdoc == null then break + var source = mdoc.location.file + if source == null then break + var path = source.filename + var stat = path.file_stat + if stat == null then break + if not stat.is_dir then path = path.dirname + + # Get the full path to the local resource + var fulllink = path / link.to_s + stat = fulllink.file_stat + if stat == null then break + + # Get a collision-free catalog name for the resource + var hash = fulllink.md5 + var ext = fulllink.file_extension + if ext != null then hash = hash + "." + ext + + # Copy the local resource in the resource directory of the catalog + var res = catalog.outdir / "res" / hash + fulllink.file_copy_to(res) + + # Hijack the link in the html. + link = ".." / "res" / hash + super(v, link, name, comment) + return + end + + # Something went bad + catalog.modelbuilder.toolcontext.error(current_mdoc.location, "Error: cannot find local image `{link}`") + super + end + + # The registered catalog + # + # It is used to deal with relative links in images. + var catalog: Catalog is noautoinit +end + redef class Catalog + redef init + do + # Register `self` to the global NitdocDecorator + # FIXME this is ugly. But no better idea at the moment. + modelbuilder.model.nitdoc_md_processor.emitter.decorator.as(NitdocDecorator).catalog = self + end + + # The output directory where to generate pages + var outdir: String is noautoinit + # Return a empty `CatalogPage`. fun new_page(rootpath: String): CatalogPage do @@ -519,6 +580,9 @@ end var out = opt_dir.value or else "catalog.out" (out/"p").mkdir +(out/"res").mkdir + +catalog.outdir = out # Generate the css (hard coded) var css = """ -- 1.7.9.5