Skip to content

Instantly share code, notes, and snippets.

@dohyunkim
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save dohyunkim/a628724e6f8a07b6f7d5 to your computer and use it in GitHub Desktop.

Select an option

Save dohyunkim/a628724e6f8a07b6f7d5 to your computer and use it in GitHub Desktop.
include web graphics
local err,warn,info,log = luatexbase.provides_module({
name = 'webgraphics',
date = '2014/06/30',
version = 0.1,
description = 'include graphics on the web',
author = 'Dohyun Kim',
license = 'public domain',
})
webgraphics = webgraphics or {}
local webgraphics = webgraphics
local http = require("socket.http")
local ltn12 = require("ltn12")
local lfs = require("lfs")
local function dowebgraphics (wfile,opts)
local lfile = string.gsub(wfile,".*/","")
lfile = string.gsub(lfile,"[^%w%.]","_")
if not lfs.isfile(lfile) then
local fh = io.open(lfile,"wb")
http.request {
url = wfile,
sink = ltn12.sink.file(fh),
}
end
tex.sprint(string.format("\\includegraphics%s{%s}",opts,lfile))
end
webgraphics.dowebgraphics = dowebgraphics
\begingroup\expandafter\expandafter\expandafter\endgroup
\expandafter\ifx\csname ProvidesPackage\endcsname\relax\else
\ProvidesPackage{webgraphics}[2014/06/30 v0.1 include graphics on the web]
\fi
\RequirePackage{luatexbase}
\RequirePackage{graphicx}
\RequireLuaModule{webgraphics}
\edef\catcodeofATchar{\catcode`@=\the\catcode`@\relax}
\catcode`@=11
\protected\def\includewebgraphics#1#{%
\toks@{#1}%
\begingroup
\catcode`\$=12 \catcode`\&=12 \catcode`\#=12 \catcode`\^=12
\catcode`\_=12 \catcode`\~=12 \catcode`\%=12 \includewebgraphicsXX
}
\def\includewebgraphicsXX#1{%
\endgroup
\directlua{webgraphics.dowebgraphics([==[#1]==], [==[\the\toks@]==])}%
}
\catcodeofATchar
\endinput
%%%%% example usage %%%%%
\includewebgraphics
[width=.3\textwidth]
{http://www.ktug.org/xe/files/attach/images/1400/400/001/KTS-meta-3-2.png}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment