Skip to content

Commit

Permalink
Merge pull request #34 from dohyunkim/master
Browse files Browse the repository at this point in the history
v2.5.3
  • Loading branch information
dohyunkim committed Mar 2, 2014
2 parents 9898207 + ca8fca2 commit 7990d6b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 26 deletions.
7 changes: 7 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
History of the luamplib package

2014/03/01 2.5.3
* using \mplibcachedir{<directory path>}, users can change the
directory for storing cache files. By default, it is the directory
where pdf output file is saved.
* at the end of package loading, luamplib searches 'luamplib.cfg',
and, when found, reads the file in automatically.

2014/02/24 2.5.2
* new macros \mplibmakenocache{...} and \mplibcancelnocache{...} to
register/unregister those .mp files that do not need caches.
Expand Down
118 changes: 92 additions & 26 deletions luamplib.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ See source file '\inFileName' for licencing and contact information.
%<*driver>
\NeedsTeXFormat{LaTeX2e}
\ProvidesFile{luamplib.drv}%
[2014/02/24 v2.5.2 Interface for using the mplib library]%
[2014/03/01 v2.5.3 Interface for using the mplib library]%
\documentclass{ltxdoc}
\usepackage{metalogo,multicol,mdwlist,fancyvrb,xspace}
\usepackage[x11names]{xcolor}
Expand Down Expand Up @@ -154,7 +154,7 @@ See source file '\inFileName' for licencing and contact information.
% \author{Hans Hagen, Taco Hoekwater, Elie Roux, Philipp Gesang and Kim Dohyun\\
% Maintainer: LuaLaTeX Maintainers ---
% Support: \email{[email protected]}}
% \date{2014/02/24 v2.5.2}
% \date{2014/03/01 v2.5.3}
%
% \maketitle
%
Expand Down Expand Up @@ -261,9 +261,20 @@ See source file '\inFileName' for licencing and contact information.
% \item |\mplibmakenocache{<filename>[,<filename>,...]}|
% \item |\mplibcancelnocache{<filename>[,<filename>,...]}|
% \end{itemize}
% where |<filename>| is a filename excluding |.mp| extension.
% where |<filename>| is a file name excluding |.mp| extension.
% Note that |.mp| files under |[TEXMFMAIN]/metapost/base| and
% |[TEXMFMAIN]/metapost/context/base| are already registered by default.
% \item By default, cache files will be stored in the same directory as
% pdf output file. This however can be changed by the command
% |\mplibcachedir{<directory path>}|, where tilde (|~|) is interpreted
% as the user's home directory (on windows machines as well).
% As backslashes (|\|) should be escaped by users, it is easier to use
% slashes (|/|) instead.
% \item At the end of package loading, \textsf{luamplib} searches
% |luamplib.cfg| and, if found, reads the file in automatically.
% Frequently used settings such as |\everymplib| or |\mplibcachedir|
% are suitable for going into this file.
%
% \end{itemize}
%
% There are (basically) two formats for metapost: \emph{plain} and
Expand Down Expand Up @@ -296,8 +307,8 @@ luamplib.lastlog = ""

local err, warn, info, log = luatexbase.provides_module({
name = "luamplib",
version = "2.5.2",
date = "2014/02/24",
version = "2.5.3",
date = "2014/03/01",
description = "Lua package to typeset Metapost with LuaTeX's MPLib.",
})

Expand All @@ -321,6 +332,11 @@ local mplib = require ('mplib')
local kpse = require ('kpse')
local lfs = require ('lfs')

local lfsattributes = lfs.attributes
local lfsisdir = lfs.isdir
local lfstouch = lfs.touch
local ioopen = io.open

local file = file
if not file then

Expand All @@ -347,8 +363,40 @@ end
% \end{macrocode}
% |btex ... etex| in input .mp files will be replaced in finder.
% \begin{macrocode}
local luamplibchange = kpse.find_file("luamplib.lua")
luamplibchange = luamplibchange and lfs.attributes(luamplibchange,"modification")
local luamplibtime = kpse.find_file("luamplib.lua")
luamplibtime = luamplibtime and lfsattributes(luamplibtime,"modification")

local currenttime = os.time()

local outputdir = "."
for _,v in ipairs(arg) do
local t = stringmatch(v,"%-output%-directory=(.+)")
if t then
outputdir = t
break
end
end

function luamplib.getcachedir(dir)
dir = stringgsub(dir,"##","#")
dir = stringgsub(dir,"^~",
os.type == "windows" and os.getenv("UserProfile") or os.getenv("HOME"))
if lfstouch and dir then
if lfsisdir(dir) then
local tmp = dir.."/_luam_plib_temp_file_"
local fh = ioopen(tmp,"w")
if fh then
fh:close(fh)
os.remove(tmp)
luamplib.cachedir = dir
else
warn("Directory '"..dir.."' is not writable!")
end
else
warn("Directory '"..dir.."' does not exist!")
end
end
end

local noneedtoreplace = {
["boxes.mp"] = true,
Expand Down Expand Up @@ -389,11 +437,11 @@ local noneedtoreplace = {
}
luamplib.noneedtoreplace = noneedtoreplace

local function replaceformatmp(file,newfile)
local fh = io.open(file,"r")
local function replaceformatmp(file,newfile,ofmodify)
local fh = ioopen(file,"r")
if not fh then return file end
local data = fh:read("*all"); fh:close()
fh = io.open(newfile,"w")
fh = ioopen(newfile,"w")
if not fh then return file end
fh:write(
"let normalinfont = infont;\n",
Expand All @@ -403,26 +451,25 @@ local function replaceformatmp(file,newfile)
"vardef Fexp_(expr x) = rawtextext(\"$^{\"&decimal x&\"}$\") enddef;\n",
"let infont = normalinfont;\n"
); fh:close()
lfstouch(newfile,currenttime,ofmodify)
return newfile
end

local function replaceinputmpfile (name,file)
local newfile = stringgsub(name,"%.mp$","")
newfile = stringgsub(newfile,"%W","_")
newfile = "luamplib_input_"..newfile..".mp"
if newfile and luamplibchange then
local ofchange = lfs.attributes(file,"modification")
local nf = lfs.attributes(newfile)
local nfchange = nf and nf.modification
if ofchange and nfchange and nf.mode == "file"
and ofchange < nfchange and luamplibchange < nfchange
then
return newfile
local ofmodify = lfsattributes(file,"modification")
if not ofmodify then return file end
local cachedir = luamplib.cachedir or outputdir
local newfile = stringgsub(name,"%W","_")
newfile = cachedir .."/luamplib_input_"..newfile
if newfile and luamplibtime then
local nf = lfsattributes(newfile)
if nf and nf.mode == "file" and ofmodify == nf.modification and luamplibtime < nf.access then
return nf.size == 0 and file or newfile
end
end
if name == "format.mp" then return replaceformatmp(file,newfile) end
if name == "format.mp" then return replaceformatmp(file,newfile,ofmodify) end

local fh = io.open(file,"r")
local fh = ioopen(file,"r")
if not fh then return file end
local data = fh:read("*all"); fh:close()
data = stringgsub(data, "\"[^\n]-\"",
Expand All @@ -447,13 +494,19 @@ local function replaceinputmpfile (name,file)
count = count + cnt
if count == 0 then
noneedtoreplace[name] = true
fh = ioopen(newfile,"w");
if fh then
fh:close()
lfstouch(newfile,currenttime,ofmodify)
end
return file
end
data = stringgsub(data,"([bem])***T***E***X***","%1tex")
data = stringgsub(data,"*****PERCENT*****","%%")
fh = io.open(newfile,"w")
fh = ioopen(newfile,"w")
if not fh then return file end
fh:write(data); fh:close()
lfstouch(newfile,currenttime,ofmodify)
return newfile
end
% \end{macrocode}
Expand All @@ -471,7 +524,9 @@ local function finder(name, mode, ftype)
else
local file = mpkpse:find_file(name,ftype)
if file then
if ftype ~= "mp" or noneedtoreplace[name] then return file end
if not lfstouch or ftype ~= "mp" or noneedtoreplace[name] then
return file
end
return replaceinputmpfile(name,file)
end
return mpkpse:find_file(name,stringmatch(name,"[a-zA-Z]+$"))
Expand Down Expand Up @@ -1328,7 +1383,7 @@ luamplib.colorconverter = colorconverter
\else
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{luamplib}
[2014/02/24 v2.5.2 mplib package for LuaTeX]
[2014/03/01 v2.5.3 mplib package for LuaTeX]
\RequirePackage{luatexbase-modutils}
\RequirePackage{pdftexcmds}
\fi
Expand Down Expand Up @@ -1495,6 +1550,7 @@ luamplib.colorconverter = colorconverter
\fi
\fi
}
\def\mplibcachedir#1{\directlua{luamplib.getcachedir("\unexpanded{#1}")}}
% \end{macrocode}
%
% We use a dedicated scratchbox.
Expand Down Expand Up @@ -1563,6 +1619,16 @@ luamplib.colorconverter = colorconverter
}
% \end{macrocode}
%
% input luamplib.cfg when it exists
%
% \begin{macrocode}
\openin0=luamplib.cfg
\ifeof0 \else
\closein0
\input luamplib.cfg
\fi
% \end{macrocode}
%
% That's all folks!
%
% \begin{macrocode}
Expand Down

0 comments on commit 7990d6b

Please sign in to comment.