| 1 |
#!/usr/bin/env texlua |
| 2 |
|
| 3 |
local show_pdf_tags_version = "1.5" |
| 4 |
|
| 5 |
kpse.set_program_name'lualatex' |
| 6 |
|
| 7 |
local mypath = string.match(debug.getinfo(1, 'S').source, '@(.*)[/\\][^/\\]+') |
| 8 |
if mypath then |
| 9 |
package.path = mypath .. '/?.lua;' .. package.path |
| 10 |
end |
| 11 |
|
| 12 |
local out_format = "tree" |
| 13 |
local follow_rolemap = false |
| 14 |
local hide_w3c = false |
| 15 |
local show_xmp = false |
| 16 |
|
| 17 |
local pdfe = pdfe or require'pdfe' |
| 18 |
local process_stream = require'show-pdf-tags_process_stream' |
| 19 |
local text_string_to_utf8 = require'show-pdf-tags_decode'.text_string_to_utf8 |
| 20 |
|
| 21 |
local function ordered_pairs(t) |
| 22 |
local keys = {} |
| 23 |
local n = 0 |
| 24 |
for k in pairs(t) do |
| 25 |
n = n + 1 |
| 26 |
keys[n] = k |
| 27 |
end |
| 28 |
table.sort(keys) |
| 29 |
local i = 0 |
| 30 |
return function() |
| 31 |
i = i + 1 |
| 32 |
local key = keys[i] |
| 33 |
if key == nil then |
| 34 |
return |
| 35 |
end |
| 36 |
return key, t[key] |
| 37 |
end |
| 38 |
end |
| 39 |
|
| 40 |
local function almost_resolve(t, v, i) |
| 41 |
local id |
| 42 |
while t == 10 do |
| 43 |
id = i |
| 44 |
t, v, i = pdfe.getfromreference(v) |
| 45 |
end |
| 46 |
return id, t, v, i |
| 47 |
end |
| 48 |
|
| 49 |
local function get_page(elem) |
| 50 |
local id, t, v, i = almost_resolve(pdfe.getfromdictionary(elem, 'Pg')) |
| 51 |
if not t or t < 2 then return end |
| 52 |
assert(id and t == 8, 'page should be a dictionary') |
| 53 |
return id |
| 54 |
end |
| 55 |
|
| 56 |
local convert_kids |
| 57 |
|
| 58 |
local function with_warnings(result) |
| 59 |
return function(ctx, warning_generator) |
| 60 |
local warnings = {} |
| 61 |
warning_generator(function(condition, warning) |
| 62 |
if condition then |
| 63 |
warnings[#warnings + 1] = warning |
| 64 |
end |
| 65 |
end, result) |
| 66 |
ctx.warnings[result] = warnings[1] and warnings |
| 67 |
return result |
| 68 |
end |
| 69 |
end |
| 70 |
|
| 71 |
local warnings_key = {} -- Just a marker, always empty |
| 72 |
local function convert_mc(ctx, mcid, page, stream_id, stream, owner) |
| 73 |
local stream_or_page_id = stream_id or page |
| 74 |
local pageno = ctx.pagenos[page] |
| 75 |
local stream_data = ctx.streams[stream_or_page_id] |
| 76 |
if stream_or_page_id and not ctx.streams[stream_or_page_id] then |
| 77 |
local warnings |
| 78 |
stream_data, warnings = process_stream( |
| 79 |
stream or ctx.document.Pages[ctx.pagenos[page]].Contents, |
| 80 |
stream and stream.Resources or ctx.document.Pages[ctx.pagenos[page]].Resources |
| 81 |
) |
| 82 |
stream_data[warnings_key] = warnings |
| 83 |
ctx.streams[stream_or_page_id] = stream_data |
| 84 |
end |
| 85 |
return with_warnings { |
| 86 |
type = 'MCR', |
| 87 |
page = pageno, |
| 88 |
stream = stream, |
| 89 |
owner = owner, |
| 90 |
content = stream_data[mcid], |
| 91 |
} (ctx, function(warn) |
| 92 |
warn(not page, 'Missing page reference in marked content reference') |
| 93 |
warn(page and not pageno, 'Page referenced in marked content referene does not exist') |
| 94 |
warn(not stream_data, 'No stream referenced in marked content reference') |
| 95 |
warn(stream_data and not stream_data[mcid], 'Referenced marked content sequence not found') |
| 96 |
local warnings = stream_data and stream_data[warnings_key][mcid] |
| 97 |
if warnings then |
| 98 |
for _, warning in ipairs(warnings) do |
| 99 |
warn(true, warning) |
| 100 |
end |
| 101 |
end |
| 102 |
end) |
| 103 |
end |
| 104 |
|
| 105 |
local function convert_objr(ctx, obj, page) |
| 106 |
local id, _, obj = assert(almost_resolve(pdfe.getfromdictionary(obj, 'Obj'))) |
| 107 |
return { |
| 108 |
type = 'OBJR', |
| 109 |
page = ctx.pagenos[page],--assert(ctx.pagenos[page]), -- TODO: assert(...) once tagpdf is adapted |
| 110 |
ObjId = id, |
| 111 |
Obj = obj, |
| 112 |
} |
| 113 |
end |
| 114 |
|
| 115 |
local default_namespace = 'http://iso.org/pdf/ssn' |
| 116 |
-- this prefix to be confirmed, another possibility would be data:, |
| 117 |
local owner_prefix = 'http://iso.org/pdf/ssn/' |
| 118 |
|
| 119 |
local function get_string(container, index, warnings) |
| 120 |
local text = pdfe.getstring(container, index, true) |
| 121 |
if text then |
| 122 |
local u = text_string_to_utf8:match(text) |
| 123 |
if u == nil then |
| 124 |
warnings[#warnings + 1] = warning |
| 125 |
return "??" |
| 126 |
end |
| 127 |
return u |
| 128 |
else |
| 129 |
return |
| 130 |
end |
| 131 |
end |
| 132 |
|
| 133 |
local function pdf2lua(container, index, t, v, x, warnings) |
| 134 |
local saved = {} |
| 135 |
local function recurse(container, index, t, v, x) |
| 136 |
if t == 10 then |
| 137 |
local id |
| 138 |
id, t, v, x = almost_resolve(t, v, x) |
| 139 |
local result = saved[id] |
| 140 |
if result == nil then |
| 141 |
result = recurse(container, index, t, v, x) |
| 142 |
saved[id] = result |
| 143 |
end |
| 144 |
return result |
| 145 |
end |
| 146 |
if not t or t < 2 then |
| 147 |
return |
| 148 |
elseif t < 6 then |
| 149 |
return v |
| 150 |
elseif t == 6 then |
| 151 |
return get_string(container, index, warnings) |
| 152 |
elseif t == 7 then |
| 153 |
local arr = {} |
| 154 |
for i=1, #v do |
| 155 |
arr[i] = recurse(v, i, pdfe.getfromarray(v, i)) |
| 156 |
end |
| 157 |
return arr |
| 158 |
elseif t == 8 then |
| 159 |
local dict = {} |
| 160 |
for i=1, #v do |
| 161 |
local k, inner_t, inner_v, detail = pdfe.getfromdictionary(v, i) |
| 162 |
dict[k] = recurse(v, k, inner_t, inner_v, detail) |
| 163 |
end |
| 164 |
return dict |
| 165 |
else |
| 166 |
assert(false, 'Streams are not handled at the moment') |
| 167 |
end |
| 168 |
end |
| 169 |
return recurse(container, index, t, v, x), warnings[0] and warnings |
| 170 |
end |
| 171 |
|
| 172 |
local function convert_attributes(ctx, attrs, classes, warnings) |
| 173 |
if not classes and not attrs then return end |
| 174 |
local attributes = {} |
| 175 |
local function apply_attr(attr) |
| 176 |
local owner = assert(attr.O) |
| 177 |
if owner == 'NSO' then |
| 178 |
-- avoid error if no-namespace attributes to be modelled by missing NS field |
| 179 |
owner = (attr.NS and get_string(attr.NS, 'NS', warnings)) or "" |
| 180 |
else |
| 181 |
owner = owner_prefix .. owner |
| 182 |
end |
| 183 |
local owner_dict = attributes[owner] |
| 184 |
if not owner_dict then |
| 185 |
owner_dict = {} |
| 186 |
attributes[owner] = owner_dict |
| 187 |
end |
| 188 |
for i = 1, #attr do |
| 189 |
local key, t, v, extra = pdfe.getfromdictionary(attr, i) |
| 190 |
if key ~= 'O' and key ~= 'NS' then |
| 191 |
owner_dict[key] = pdf2lua(attr, key, t, v, extra, warnings) |
| 192 |
end |
| 193 |
end |
| 194 |
end |
| 195 |
local function apply_attrs(attrs) |
| 196 |
if attrs == nil then return end |
| 197 |
local t = pdfe.type(attrs) |
| 198 |
if t == 'pdfe.dictionary' then |
| 199 |
apply_attr(attrs) |
| 200 |
else |
| 201 |
assert(t == 'pdfe.array') |
| 202 |
for i=1, #attrs do |
| 203 |
local attr = attrs[i] |
| 204 |
if type(attr) ~= 'number' then |
| 205 |
apply_attr(attr) |
| 206 |
end |
| 207 |
end |
| 208 |
end |
| 209 |
end |
| 210 |
if classes then |
| 211 |
if type(classes) == 'string' then |
| 212 |
apply_attrs(ctx.ClassMap[classes]) |
| 213 |
else |
| 214 |
for i=1, #classes do |
| 215 |
local class = classes[i] |
| 216 |
if type(class) ~= 'number' then |
| 217 |
apply_attrs(ctx.ClassMap[classes[i]]) |
| 218 |
end |
| 219 |
end |
| 220 |
end |
| 221 |
end |
| 222 |
if attrs then |
| 223 |
apply_attrs(attrs) |
| 224 |
end |
| 225 |
return attributes |
| 226 |
end |
| 227 |
|
| 228 |
local function convert(ctx, elem, id, page) |
| 229 |
if type(elem) == 'number' then |
| 230 |
return convert_mc(ctx, elem, page) |
| 231 |
elseif elem.Type == 'MCR' then |
| 232 |
local stm_id, _, stm = almost_resolve(pdfe.getfromdictionary(elem, 'Stm')) |
| 233 |
return convert_mc(ctx, elem.MCID, get_page(elem) or page, stm_id, stm, elem.StmOwn) |
| 234 |
elseif elem.Type == 'OBJR' then |
| 235 |
return convert_objr(ctx, elem, get_page(elem) or page) |
| 236 |
end |
| 237 |
local warnings = {} |
| 238 |
local ns = elem.NS |
| 239 |
local role_mapped_s, role_mapped_ns |
| 240 |
ns = ns and get_string(ns, 'NS', warnings) or default_namespace |
| 241 |
local obj = { |
| 242 |
subtype = ctx.type_maps[elem.NS and tostring(elem.NS) or false][elem.S], |
| 243 |
attributes = convert_attributes(ctx, elem.A, elem.C, warnings), |
| 244 |
title = get_string(elem, 'T', warnings), |
| 245 |
lang = get_string(elem, 'Lang', warnings), |
| 246 |
alt = get_string(elem, 'Alt', warnings), |
| 247 |
expanded = get_string(elem, 'E', warnings), |
| 248 |
actual_text = get_string(elem, 'ActualText', warnings), |
| 249 |
associated_files = elem.AF, |
| 250 |
id = get_string(elem, 'ID', warnings), |
| 251 |
phoneme = get_string(elem, 'Phoneme', warnings), |
| 252 |
phonetic_alphabet = get_string(elem, 'PhoneticAlphabet', warnings), |
| 253 |
kids = convert_kids(ctx, elem), |
| 254 |
} |
| 255 |
if warnings[1] then |
| 256 |
ctx.warnings[obj] = warnings |
| 257 |
end |
| 258 |
if id then |
| 259 |
ctx.id_map[id] = obj |
| 260 |
end |
| 261 |
local elem_ref = elem.Ref |
| 262 |
if elem_ref and #elem_ref > 0 then |
| 263 |
local ref = {} |
| 264 |
for i = 1, #elem_ref do |
| 265 |
ref[i] = assert(almost_resolve(pdfe.getfromarray(elem_ref, i))) |
| 266 |
end |
| 267 |
obj.ref = ref |
| 268 |
ctx.ref_entries[#ctx.ref_entries + 1] = obj |
| 269 |
end |
| 270 |
return obj |
| 271 |
end |
| 272 |
|
| 273 |
function convert_kids(ctx, elem) |
| 274 |
local id, t, k = almost_resolve(pdfe.getfromdictionary(elem, 'K')) |
| 275 |
if not k then return nil end |
| 276 |
local page = get_page(elem) |
| 277 |
if t == 7 then |
| 278 |
local result = {} |
| 279 |
for i = 1, #k do |
| 280 |
local id, t, kid = almost_resolve(pdfe.getfromarray(k, i)) |
| 281 |
result[i] = convert(ctx, k[i], id, page) |
| 282 |
end |
| 283 |
return result |
| 284 |
else |
| 285 |
return {convert(ctx, k, id, page)} |
| 286 |
end |
| 287 |
end |
| 288 |
|
| 289 |
local function role_map_to_lua(role_map) |
| 290 |
if not role_map then return {} end |
| 291 |
local lua_role_map = pdfe.dictionarytotable(role_map) |
| 292 |
for k, mapping in pairs(lua_role_map) do |
| 293 |
if mapping[1] == 5 then -- name |
| 294 |
mapping[1], mapping[2] = mapping[2], false |
| 295 |
elseif mapping[1] == 7 then -- array |
| 296 |
if mapping[3] == 2 then |
| 297 |
mapping[1], mapping[2], mapping[3] = mapping[2][1], mapping[2][2] |
| 298 |
else |
| 299 |
io.stderr:write"Ignoring entry with invalid length in rolemap\n" |
| 300 |
end |
| 301 |
else |
| 302 |
io.stderr:write"Ignoring invalid rolemap entry\n" |
| 303 |
end |
| 304 |
end |
| 305 |
return lua_role_map |
| 306 |
end |
| 307 |
|
| 308 |
local function open(filename) |
| 309 |
local document = pdfe.open(filename) |
| 310 |
if 0 < (pdfe.getstatus(document) or 2) then |
| 311 |
return nil, 'Failed to open document' |
| 312 |
end |
| 313 |
local ctx = { |
| 314 |
document = document, |
| 315 |
streams = {}, |
| 316 |
} |
| 317 |
|
| 318 |
local catalog = pdfe.getcatalog(document) |
| 319 |
local markinfo = catalog and catalog.MarkInfo |
| 320 |
local tagged = markinfo and markinfo.Marked |
| 321 |
local xmp = catalog and catalog.Metadata |
| 322 |
|
| 323 |
if not tagged then |
| 324 |
io.stderr:write("Document catalog has no markinfo.Marked entry. It might not be tagged.\n") |
| 325 |
end |
| 326 |
|
| 327 |
local pagenos = {} |
| 328 |
for i, page in ipairs(pdfe.pagestotable(document)) do |
| 329 |
pagenos[page[3]] = i |
| 330 |
end |
| 331 |
ctx.pagenos = pagenos |
| 332 |
|
| 333 |
local id_map = {} |
| 334 |
ctx.id_map = id_map |
| 335 |
ctx.ref_entries = {} |
| 336 |
|
| 337 |
ctx.xmp = xmp |
| 338 |
|
| 339 |
local structroot = catalog.StructTreeRoot |
| 340 |
if not structroot then |
| 341 |
return {}, ctx |
| 342 |
end |
| 343 |
local type_maps = {} |
| 344 |
ctx.warnings = setmetatable({}, {__mode = 'k'}) |
| 345 |
local global_warnings = {} |
| 346 |
do |
| 347 |
local namespaces = structroot.Namespaces |
| 348 |
for i=0, namespaces and #namespaces or 0 do |
| 349 |
local ns, ns_key, role_map |
| 350 |
if i == 0 then |
| 351 |
ns, ns_key = false, false |
| 352 |
role_map = structroot.RoleMap |
| 353 |
else |
| 354 |
local namespace = namespaces[i] |
| 355 |
ns = get_string(namespace, 'NS', global_warnings) |
| 356 |
ns_key = tostring(namespace) |
| 357 |
role_map = namespace.RoleMapNS |
| 358 |
end |
| 359 |
role_map = role_map_to_lua(role_map) |
| 360 |
type_maps[ns_key] = setmetatable({}, {__index = function(t, elem) |
| 361 |
local element = {subtype = elem, namespace = ns} |
| 362 |
t[elem] = element |
| 363 |
|
| 364 |
local mapped = role_map[elem] |
| 365 |
if mapped then |
| 366 |
element.mapped = type_maps[mapped[2] and tostring(mapped[2])][mapped[1]] |
| 367 |
end |
| 368 |
return element |
| 369 |
end}) |
| 370 |
end |
| 371 |
end |
| 372 |
if global_warnings[1] then warnings[false] = global_warnings end |
| 373 |
ctx.type_maps = type_maps |
| 374 |
ctx.ClassMap = structroot.ClassMap |
| 375 |
local elements = convert_kids(ctx, structroot) |
| 376 |
ctx.ClassMap = nil |
| 377 |
|
| 378 |
for _, obj in ipairs(ctx.ref_entries) do |
| 379 |
local refs = obj.ref |
| 380 |
for i, ref in ipairs(refs) do |
| 381 |
refs[i] = assert(id_map[ref]) |
| 382 |
end |
| 383 |
end |
| 384 |
ctx.ref_entries = nil |
| 385 |
|
| 386 |
return elements, ctx |
| 387 |
end |
| 388 |
|
| 389 |
local function mark_references(tree) |
| 390 |
local count = 0 |
| 391 |
local referenced = {} |
| 392 |
local function recurse(objs) |
| 393 |
for _, obj in ipairs(objs) do |
| 394 |
if obj.ref then |
| 395 |
for _, ref in ipairs(obj.ref) do |
| 396 |
if not referenced[ref] then |
| 397 |
count = count + 1 |
| 398 |
referenced[ref] = count |
| 399 |
end |
| 400 |
end |
| 401 |
end |
| 402 |
if obj.kids then |
| 403 |
recurse(obj.kids) |
| 404 |
end |
| 405 |
end |
| 406 |
end |
| 407 |
recurse(tree) |
| 408 |
return referenced, count |
| 409 |
end |
| 410 |
|
| 411 |
local function format_subtype(subtype) |
| 412 |
if subtype.namespace then |
| 413 |
return string.format('%s (%s)', subtype.subtype, subtype.namespace) |
| 414 |
else |
| 415 |
return subtype.subtype |
| 416 |
end |
| 417 |
end |
| 418 |
|
| 419 |
local function format_subtype_xml(subtype) |
| 420 |
if subtype.namespace then |
| 421 |
return string.format('<%s xmlns="%s"', subtype.subtype, |
| 422 |
(hide_w3c and subtype.namespace:gsub('http://www.w3.org', 'http://-www.w3.org')) or subtype.namespace) |
| 423 |
else |
| 424 |
return "<" .. subtype.subtype:gsub(":","_x3A_") |
| 425 |
end |
| 426 |
end |
| 427 |
|
| 428 |
local function print_tree(tree, ctx) |
| 429 |
local referenced = mark_references(tree) |
| 430 |
local function recurse(objs, first_prefix, last_first_prefix, prefix, last_prefix) |
| 431 |
for i, obj in ipairs(objs) do |
| 432 |
local warnings = ctx.warnings[obj] |
| 433 |
if warnings then |
| 434 |
print'### Warnings encountered:' |
| 435 |
for _, warning in ipairs(warnings) do |
| 436 |
print('# ' .. warning) |
| 437 |
end |
| 438 |
end |
| 439 |
if i == #objs then first_prefix, prefix = last_first_prefix, last_prefix end |
| 440 |
if obj.type == 'MCR' then |
| 441 |
print(string.format('%sMarked content on page %i: %s', first_prefix, obj.page or -1, obj.content or '')) |
| 442 |
elseif obj.type == 'OBJR' then |
| 443 |
local t = obj.Obj.Type |
| 444 |
t = t and string.format(' of type %s', t) or '' |
| 445 |
local page = obj.page |
| 446 |
page = page and string.format(' on page %i', page) or '' -- TODO: Should eventually become always true |
| 447 |
print(string.format('%sReferenced object%s%s', first_prefix, t, page)) |
| 448 |
else |
| 449 |
local mark = obj.kids and ':' or '' |
| 450 |
local subtype = obj.subtype |
| 451 |
local mapped = subtype.mapped |
| 452 |
mapped = mapped and ' / ' .. format_subtype(mapped) or '' |
| 453 |
print(string.format('%s%s%s%s', first_prefix, format_subtype(subtype), mapped, mark)) |
| 454 |
local lines = {} |
| 455 |
if referenced[obj] then |
| 456 |
lines[#lines + 1] = 'Referenced as object ' .. referenced[obj] |
| 457 |
end |
| 458 |
if obj.title then |
| 459 |
lines[#lines + 1] = 'Title: ' .. obj.title |
| 460 |
end |
| 461 |
if obj.lang then |
| 462 |
lines[#lines + 1] = 'Language: ' .. obj.lang |
| 463 |
end |
| 464 |
if obj.expanded then |
| 465 |
lines[#lines + 1] = 'Expansion: ' .. obj.expanded |
| 466 |
end |
| 467 |
if obj.alt then |
| 468 |
lines[#lines + 1] = 'Alternate text: ' .. obj.alt |
| 469 |
end |
| 470 |
if obj.actual_text then |
| 471 |
lines[#lines + 1] = 'Actual text: ' .. obj.actual_text |
| 472 |
end |
| 473 |
if obj.phoneme then |
| 474 |
lines[#lines + 1] = 'Phoneme: ' .. obj.phoneme |
| 475 |
end |
| 476 |
if obj.phonetic_alphabet then |
| 477 |
lines[#lines + 1] = 'PhoneticAlphabet: ' .. obj.phonetic_alphabet |
| 478 |
end |
| 479 |
if obj.associated_files then |
| 480 |
local af_output = '' |
| 481 |
local total_count = #af_output |
| 482 |
for i, file in ipairs(obj.associated_files) do |
| 483 |
if file.EF.F then |
| 484 |
af_output = '\n└─Content: ' .. pdfe.readwholestream(file.EF.F, true):gsub('\n', '\n ') |
| 485 |
-- recurse(obj.kids, prefix .. '├─', prefix .. '└─', prefix .. '│ ', prefix .. ' ') |
| 486 |
end |
| 487 |
end |
| 488 |
lines[#lines + 1] = 'Associated files are present:' .. af_output |
| 489 |
end |
| 490 |
if obj.attributes then |
| 491 |
local owners = {} |
| 492 |
for k in next, obj.attributes do |
| 493 |
owners[#owners + 1] = k |
| 494 |
end |
| 495 |
table.sort(owners) |
| 496 |
for i=1, #owners do |
| 497 |
local attrs = {} |
| 498 |
for k in next, obj.attributes[owners[i]] do |
| 499 |
attrs[#attrs + 1] = k |
| 500 |
end |
| 501 |
table.sort(attrs) |
| 502 |
for j=1, #attrs do |
| 503 |
attrs[j] = attrs[j] .. ': ' .. require'show-pdf-tags-inspect'(obj.attributes[owners[i]][attrs[j]]) |
| 504 |
end |
| 505 |
table.insert(attrs, 1, (owners[i]:sub(1, #owner_prefix) == owner_prefix and '/' .. owners[i]:sub(#owner_prefix+1) or owners[i]) .. ':') |
| 506 |
for j=1, #attrs-1 do |
| 507 |
attrs[j] = attrs[j]:gsub('\n', '\n│') |
| 508 |
end |
| 509 |
owners[i] = table.concat(attrs, '\n├', 1, #attrs-1) .. '\n└' .. attrs[#attrs]:gsub('\n', '\n ') |
| 510 |
end |
| 511 |
table.insert(owners, 1, 'Attributes: ') |
| 512 |
for j=1, #owners-1 do |
| 513 |
owners[j] = owners[j]:gsub('\n', '\n│') |
| 514 |
end |
| 515 |
lines[#lines + 1] = table.concat(owners, '\n├', 1, #owners-1) .. '\n└' .. owners[#owners]:gsub('\n', '\n ') |
| 516 |
end |
| 517 |
-- attributes = convert_attributes(elem.A), |
| 518 |
-- attribute_classes = convert_attribute_classes(elem.C), |
| 519 |
if obj.ref then |
| 520 |
local refs = {} |
| 521 |
for i, r in ipairs(obj.ref) do |
| 522 |
refs[i] = referenced[r] |
| 523 |
end |
| 524 |
lines[#lines + 1] = 'References object' .. (refs[2] and 's' or '') .. ' ' .. table.concat(refs, ', ') |
| 525 |
end |
| 526 |
if obj.kids then |
| 527 |
for _, l in ipairs(lines) do |
| 528 |
print(prefix .. '┝━━' .. l:gsub('\n', '\n' .. prefix .. '│ ')) |
| 529 |
end |
| 530 |
recurse(obj.kids, prefix .. '├─', prefix .. '└─', prefix .. '│ ', prefix .. ' ') |
| 531 |
elseif #lines > 0 then |
| 532 |
for i=1, #lines-1 do |
| 533 |
print(prefix .. '┝━━' .. lines[i]:gsub('\n', '\n' .. prefix .. '│ ')) |
| 534 |
end |
| 535 |
print(prefix .. '┕━━' .. lines[#lines]:gsub('\n', '\n' .. prefix .. ' ')) |
| 536 |
end |
| 537 |
end |
| 538 |
end |
| 539 |
end |
| 540 |
return recurse(tree, '', '', '', '') |
| 541 |
end |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
local function print_tree_xml(tree, ctx) |
| 547 |
local referenced = mark_references(tree) |
| 548 |
local function recurse(objs, indent) |
| 549 |
for i, obj in ipairs(objs) do |
| 550 |
local warnings = ctx.warnings[obj] |
| 551 |
if warnings then |
| 552 |
for _, warning in ipairs(warnings) do |
| 553 |
print(string.format('%s<!-- %s -->', indent, warning)) |
| 554 |
end |
| 555 |
end |
| 556 |
if obj.type == 'MCR' then |
| 557 |
print(string.format('%s<?MarkedContent page="%i" ?>%s', indent, obj.page or -1, (obj.content and obj.content:gsub('&','&'):gsub('<','<'):gsub('\0','[NULL]'):gsub('[\1-\8\11\12\14-\31]','[CTRL]'):gsub('�.*','[TEXT]') or "[missing]"))) |
| 558 |
elseif obj.type == 'OBJR' then |
| 559 |
local t = obj.Obj.Type |
| 560 |
t = t and string.format(' type="%s"', t) or '' |
| 561 |
local page = obj.page |
| 562 |
page = page and string.format(' page="%i"', page) or '' -- TODO: Should eventually become always true |
| 563 |
print(string.format('%s<?ReferencedObject%s%s ?>', indent, t, page)) |
| 564 |
else |
| 565 |
local subtype = obj.subtype |
| 566 |
local mapped = subtype.mapped |
| 567 |
-- mapped = mapped and mapped.subtype or '' |
| 568 |
-- mapped = mapped and ' / ' .. format_subtype(mapped) or '' |
| 569 |
if follow_rolemap and mapped then |
| 570 |
print(string.format('%s%s', indent, format_subtype_xml(mapped))) |
| 571 |
else |
| 572 |
print(string.format('%s%s', indent, format_subtype_xml(subtype))) |
| 573 |
end |
| 574 |
local lines = {} |
| 575 |
if obj.id then |
| 576 |
lines[#lines + 1] = ' id="' .. obj.id:gsub('&','&'):gsub('<','<'):gsub('"','"'):gsub('\0','[NULL]') .. '"' |
| 577 |
end |
| 578 |
if obj.title then |
| 579 |
lines[#lines + 1] = ' title="' .. obj.title:gsub('&','&'):gsub('<','<'):gsub('"','"'):gsub('\0','[NULL]'):gsub('[\1-\8\11\12\14-\31]','[CTRL]') .. '"' |
| 580 |
end |
| 581 |
if obj.lang then |
| 582 |
lines[#lines + 1] = ' lang="' .. obj.lang .. '"' |
| 583 |
end |
| 584 |
if obj.expanded then |
| 585 |
lines[#lines + 1] = ' expansion="' .. obj.expanded:gsub('&','&'):gsub('<','<'):gsub('"','"'):gsub('\0','[NULL]'):gsub('[\1-\8\11\12\14-\31]','[CTRL]') .. '"' |
| 586 |
end |
| 587 |
if obj.alt then |
| 588 |
lines[#lines + 1] = ' alt="' .. obj.alt:gsub('&','&'):gsub('<','<'):gsub('"','"'):gsub('\0','[NULL]'):gsub('[\1-\8\11\12\14-\31]','[CTRL]') .. '"' |
| 589 |
end |
| 590 |
if obj.actual_text then |
| 591 |
lines[#lines + 1] = ' actualtext="' .. obj.actual_text:gsub('&','&'):gsub('<','<'):gsub('"','"'):gsub('\0','[NULL]'):gsub('[\1-\8\11\12\14-\31]','[CTRL]') .. '"' |
| 592 |
end |
| 593 |
if obj.phoneme then |
| 594 |
lines[#lines + 1] = ' phoneme="' .. obj.phoneme:gsub('&','&'):gsub('<','<'):gsub('"','"'):gsub('\0','[NULL]'):gsub('[\1-\8\11\12\14-\31]','[CTRL]') .. '"' |
| 595 |
end |
| 596 |
if obj.phonetic_alphabet then |
| 597 |
lines[#lines + 1] = ' phonetic-alphabet="' .. obj.phonetic_alphabet:gsub('&','&'):gsub('<','<'):gsub('"','"'):gsub('\0','[NULL]'):gsub('[\1-\8\11\12\14-\31]','[CTRL]') .. '"' |
| 598 |
end |
| 599 |
if obj.associated_files then |
| 600 |
local f = {} |
| 601 |
local warnings = {} |
| 602 |
for i, file in ipairs(obj.associated_files) do |
| 603 |
if file.EF.F then |
| 604 |
f[#f+1] = get_string(file, "UF", warnings) |
| 605 |
end |
| 606 |
end |
| 607 |
for _, warning in ipairs(warnings) do |
| 608 |
io.stderr:write('Warning while processing associated files: ' .. warning .. '\n') |
| 609 |
end |
| 610 |
lines[#lines + 1] = ' af="' .. table.concat(f, ' ') .. '"' |
| 611 |
end |
| 612 |
if obj.attributes then |
| 613 |
for k,v in ordered_pairs(obj.attributes) do |
| 614 |
local attrns="" |
| 615 |
if k~=subtype.namespace then |
| 616 |
attrns = k:gsub('.*/','') |
| 617 |
end |
| 618 |
if type(v) == "table" then |
| 619 |
if attrns ~= "" then |
| 620 |
lines[#lines +1] = ' xmlns:' .. attrns .. '="' .. k .. '"' |
| 621 |
attrns = attrns ..':' |
| 622 |
end |
| 623 |
for kk,vv in ordered_pairs(v) do |
| 624 |
if type(vv) == "table" then |
| 625 |
vv = require'show-pdf-tags-inspect'(vv):gsub('\n[ ]*',' ') |
| 626 |
end |
| 627 |
lines[#lines+1] = ' ' ..attrns .. kk .. '="' .. tostring(vv):gsub('&','&'):gsub('<','<'):gsub('"','"'):gsub('\0','[NULL]') .. '"' |
| 628 |
end |
| 629 |
else |
| 630 |
io.stderr:write("Unexpected attributes object\n") |
| 631 |
end |
| 632 |
end |
| 633 |
end |
| 634 |
if mapped and mapped.subtype then |
| 635 |
if follow_rolemap then |
| 636 |
if subtype.namespace then |
| 637 |
lines[#lines+1] = ' xmlns:orig-ns="' .. subtype.namespace .. '"' |
| 638 |
lines[#lines+1] = ' rolemapped-from="orig-ns:' .. subtype.subtype .. '"' |
| 639 |
else |
| 640 |
lines[#lines+1] = ' rolemapped-from="' .. subtype.subtype .. '"' |
| 641 |
end |
| 642 |
else |
| 643 |
lines[#lines+1] = ' rolemaps-to="' .. mapped.subtype .. '"' |
| 644 |
end |
| 645 |
end |
| 646 |
-- attributes = convert_attributes(elem.A), |
| 647 |
-- attribute_classes = convert_attribute_classes(elem.C), |
| 648 |
if referenced[obj] then |
| 649 |
lines[#lines + 1] = ' referenced-as="' .. referenced[obj] .. '"' |
| 650 |
end |
| 651 |
lines[#lines+1] = ">" |
| 652 |
-- |
| 653 |
if obj.associated_files then |
| 654 |
local f = {} |
| 655 |
local af_output = '' |
| 656 |
local warnings = {} |
| 657 |
for i, file in ipairs(obj.associated_files) do |
| 658 |
if file.EF.F then |
| 659 |
af_output = pdfe.readwholestream(file.EF.F, true) |
| 660 |
lines[#lines + 1] = '<AssociatedFile name="' .. get_string(file, "UF", warnings) .. '" xmlns="">' |
| 661 |
if file.EF.F.Subtype == 'application/mathml+xml' then |
| 662 |
lines[#lines + 1] = af_output |
| 663 |
else |
| 664 |
lines[#lines + 1] = af_output:gsub('&','&'):gsub('<','<'):gsub('"','"'):gsub('\0','[NULL]'):gsub('[\1-\8\11\12\14-\31]','[CTRL]') |
| 665 |
end |
| 666 |
lines[#lines + 1] = '</AssociatedFile>' |
| 667 |
end |
| 668 |
end |
| 669 |
end |
| 670 |
-- |
| 671 |
if obj.ref then |
| 672 |
local refs = {} |
| 673 |
for i, r in ipairs(obj.ref) do |
| 674 |
refs[i] = referenced[r] |
| 675 |
end |
| 676 |
lines[#lines + 1] = '<?References objects="' .. table.concat(refs, ' ') ..'" ?>' |
| 677 |
end |
| 678 |
if obj.kids then |
| 679 |
for _, l in ipairs(lines) do |
| 680 |
print(indent .. ' ' .. l:gsub('\n', '\n' .. indent .. ' ')) |
| 681 |
end |
| 682 |
recurse(obj.kids, indent .. ' ') |
| 683 |
if follow_rolemap and mapped then |
| 684 |
print(indent .. "</" .. mapped.subtype ..">") |
| 685 |
else |
| 686 |
print(indent .. "</" .. subtype.subtype:gsub(":","_x3A_") ..">") |
| 687 |
end |
| 688 |
elseif #lines > 0 then |
| 689 |
for i=1, #lines-1 do |
| 690 |
print(indent .. ' ' .. lines[i]:gsub('\n', '\n' .. indent .. ' ')) |
| 691 |
end |
| 692 |
print(indent .. ' ' .. lines[#lines]:gsub('\n', '\n' .. indent .. ' ')) |
| 693 |
if follow_rolemap and mapped then |
| 694 |
print(indent .. "</" .. mapped.subtype ..">") |
| 695 |
else |
| 696 |
print(indent .. "</" .. subtype.subtype:gsub(":","_x3A_") ..">") |
| 697 |
end |
| 698 |
end |
| 699 |
end |
| 700 |
end |
| 701 |
end |
| 702 |
print ("<PDF>\n <StructTreeRoot>") |
| 703 |
recurse(tree, ' ', '', '', ' ') |
| 704 |
print (" </StructTreeRoot>") |
| 705 |
if show_xmp then |
| 706 |
print(" <XMP>\n" ..pdfe.readwholestream(ctx.xmp,true):gsub("\n[ \n]*\n","\n") .. "\n </XMP>") |
| 707 |
end |
| 708 |
print ("</PDF>") |
| 709 |
return |
| 710 |
end |
| 711 |
|
| 712 |
|
| 713 |
local helpstr =[[ |
| 714 |
|
| 715 |
Usage: %s options <filename>.pdf |
| 716 |
Options |
| 717 |
--help|-h show this help |
| 718 |
--version|-v show the current version |
| 719 |
--tree (default) show as tree |
| 720 |
--xml show as XML |
| 721 |
--table show Lua table structure |
| 722 |
--map Follow role mapping (xml printer) |
| 723 |
--xmp include XMP XML (xml printer) |
| 724 |
--w3c- Add - to w3c namespaces to force browser tree display |
| 725 |
|
| 726 |
]] |
| 727 |
|
| 728 |
local argi = 1 |
| 729 |
while argi <= #arg and arg[argi]:match("^%-") do |
| 730 |
if arg[argi] == "--tree" then |
| 731 |
out_format="tree" |
| 732 |
elseif arg[argi] == "--xml" then |
| 733 |
out_format="xml" |
| 734 |
elseif arg[argi] == "--table" then |
| 735 |
out_format="table" |
| 736 |
elseif arg[argi] == "--map" then |
| 737 |
follow_rolemap=true |
| 738 |
elseif arg[argi] == "--w3c-" then |
| 739 |
hide_w3c=true |
| 740 |
elseif arg[argi] == "--xmp" then |
| 741 |
show_xmp=true |
| 742 |
elseif arg[argi] == "--help" or arg[argi] == "-h" then |
| 743 |
io.stderr:write(string.format(helpstr, arg[0])) |
| 744 |
return |
| 745 |
elseif arg[argi] == "--version" or arg[argi] == "-v" then |
| 746 |
io.stderr:write(string.format("show-pdf-tags version: %s\n", show_pdf_tags_version)) |
| 747 |
return |
| 748 |
else |
| 749 |
io.stderr:write(string.format('Unknown option: %s\n', arg[argi])) |
| 750 |
return |
| 751 |
end |
| 752 |
argi=argi+1 |
| 753 |
end |
| 754 |
|
| 755 |
if argi < #arg then |
| 756 |
io.stderr:write(string.format('Extra argument. Usage: %s options <filename>.pdf\n', arg[0])) |
| 757 |
return |
| 758 |
end |
| 759 |
|
| 760 |
if argi > #arg then |
| 761 |
io.stderr:write(string.format('Missing argument. Usage: %s options <filename>.pdf\n', arg[0])) |
| 762 |
return |
| 763 |
end |
| 764 |
|
| 765 |
local struct, ctx = assert(open(arg[argi])) |
| 766 |
|
| 767 |
if out_format=="tree" then |
| 768 |
print_tree(struct, ctx) |
| 769 |
else |
| 770 |
if out_format=="xml" then |
| 771 |
print_tree_xml(struct, ctx) |
| 772 |
else |
| 773 |
print(require'show-pdf-tags-inspect'(struct)) |
| 774 |
end |
| 775 |
end |