Debian Codegraph @

texmf-dist/doc/support/cluttex/src/auxfile.sml

raw
1 structure AuxFile:
2 sig
3 val createMissingDirectories:
4 {auxfile: string, outdir: string, seen: StringSet.set}
5 -> bool * StringSet.set
6 val extractBibTeXLines: {auxfile: string, outdir: string} -> string list
7 end =
8 struct
9 fun stripPrefix prefix s =
10 if String.isPrefix prefix s then
11 SOME (Substring.extract (s, String.size prefix, NONE))
12 else
13 NONE
14 fun isInput (line, outdir) =
15 case stripPrefix "\\@input{" line of
16 NONE => NONE
17 | SOME rest =>
18 let
19 val subauxfile = Substring.string
20 (Substring.takel (fn c => c <> #"}") rest)
21 val subauxfile_abs =
22 PathUtil.abspath {path = subauxfile, cwd = SOME outdir}
23 in
24 SOME {subauxfile = subauxfile, subauxfile_abs = subauxfile_abs}
25 end
26 fun createMissingDirectories {auxfile, outdir, seen} =
27 let
28 val ins = TextIO.openIn auxfile
29 val seen = StringSet.add (seen, auxfile)
30 fun go (did, seen) =
31 case TextIO.inputLine ins of
32 NONE => (TextIO.closeIn ins; (did, seen))
33 | SOME line =>
34 case isInput (line, outdir) of
35 NONE => go (did, seen)
36 | SOME {subauxfile, subauxfile_abs} =>
37 if FSUtil.isFile subauxfile_abs then
38 let
39 val (did', seen) =
40 createMissingDirectories
41 {auxfile = subauxfile_abs, outdir = outdir, seen = seen}
42 in
43 go (did orelse did', seen)
44 end
45 else
46 let
47 val dir =
48 PathUtil.join2 (outdir, PathUtil.dirname subauxfile)
49 in
50 if FSUtil.isDirectory dir then go (did, seen)
51 else (FSUtil.mkDirRec dir; go (true, seen))
52 end
53 in
54 go (false, seen)
55 end
56
57 (* \citation, \bibdata, \bibstyle *)
58 fun extractBibTeXLines' {auxfile, outdir, revLines} =
59 let
60 val ins = TextIO.openIn auxfile
61 fun go revLines =
62 case TextIO.inputLine ins of
63 NONE => (TextIO.closeIn ins; revLines)
64 | SOME line =>
65 case isInput (line, outdir) of
66 SOME {subauxfile, subauxfile_abs} =>
67 if FSUtil.isFile subauxfile_abs then
68 let
69 val revLines =
70 extractBibTeXLines'
71 { auxfile = subauxfile_abs
72 , outdir = outdir
73 , revLines = revLines
74 }
75 in
76 go revLines
77 end
78 else
79 go revLines
80 | NONE =>
81 let
82 val isBibTeXLine =
83 case stripPrefix "\\" line of
84 SOME s =>
85 (case
86 Substring.string
87 (Substring.takel
88 (fn c => Char.isAlpha c orelse c = #"@") s)
89 of
90 "citation" => true
91 | "bibdata" => true
92 | "bibstyle" => true
93 | _ => false)
94 | NONE => false
95 in
96 if isBibTeXLine then
97 ( if Message.getVerbosity () >= 2 then
98 Message.info
99 ("BibTeX line: "
100 ^
101 Substring.string
102 (Substring.dropr Char.isSpace (Substring.full line)))
103 else
104 ()
105 ; go (line :: revLines)
106 )
107 else
108 go revLines
109 end
110 in
111 go revLines
112 end
113 fun extractBibTeXLines {auxfile, outdir} =
114 List.rev
115 (extractBibTeXLines' {auxfile = auxfile, outdir = outdir, revLines = []})
116 end;