| 1 |
#!/usr/bin/env texlua |
| 2 |
-- |
| 3 |
-- File ctex-zhconv-make.lua |
| 4 |
-- |
| 5 |
-- Copyright (C) 2020 by Qing Lee <[email protected]> |
| 6 |
-------------------------------------------------------------------------- |
| 7 |
-- |
| 8 |
-- This work may be distributed and/or modified under the |
| 9 |
-- conditions of the LaTeX Project Public License, either |
| 10 |
-- version 1.3c of this license or (at your option) any later |
| 11 |
-- version. This version of this license is in |
| 12 |
-- http://www.latex-project.org/lppl/lppl-1-3c.txt |
| 13 |
-- and the latest version of this license is in |
| 14 |
-- http://www.latex-project.org/lppl.txt |
| 15 |
-- and version 1.3 or later is part of all distributions of |
| 16 |
-- LaTeX version 2005/12/01 or later. |
| 17 |
-- |
| 18 |
-- This work has the LPPL maintenance status "maintained". |
| 19 |
-- |
| 20 |
-- The Current Maintainer of this work is Qing Lee. |
| 21 |
-- |
| 22 |
-- This work consists of the files ctex-zhconv.lua, ctex-zhconv-make.lua |
| 23 |
-- and the derived file ctex-zhconv-index.lua. |
| 24 |
-- |
| 25 |
-------------------------------------------------------------------------- |
| 26 |
-- |
| 27 |
|
| 28 |
local preamble = [[ |
| 29 |
-- |
| 30 |
-- File ctex-zhconv-index.lua |
| 31 |
-- |
| 32 |
-- Copyright (C) 2020 by Qing Lee <[email protected]> |
| 33 |
-------------------------------------------------------------------------- |
| 34 |
-- |
| 35 |
-- This work may be distributed and/or modified under the |
| 36 |
-- conditions of the LaTeX Project Public License, either |
| 37 |
-- version 1.3c of this license or (at your option) any later |
| 38 |
-- version. This version of this license is in |
| 39 |
-- http://www.latex-project.org/lppl/lppl-1-3c.txt |
| 40 |
-- and the latest version of this license is in |
| 41 |
-- http://www.latex-project.org/lppl.txt |
| 42 |
-- and version 1.3 or later is part of all distributions of |
| 43 |
-- LaTeX version 2005/12/01 or later. |
| 44 |
-- |
| 45 |
-- This work has the LPPL maintenance status "maintained". |
| 46 |
-- |
| 47 |
-- The Current Maintainer of this work is Qing Lee. |
| 48 |
-- |
| 49 |
-- This work consists of the files ctex-zhconv.lua, ctex-zhconv-make.lua |
| 50 |
-- and the derived file ctex-zhconv-index.lua. |
| 51 |
-- |
| 52 |
-------------------------------------------------------------------------- |
| 53 |
-- |
| 54 |
-- Do not edit this file! |
| 55 |
-- Generated from the WHATWG Encoding Standard: |
| 56 |
-- |
| 57 |
-- https://encoding.spec.whatwg.org/index-big5.txt (Date: %s) |
| 58 |
-- https://encoding.spec.whatwg.org/index-gb18030.txt (Date: %s) |
| 59 |
-- https://encoding.spec.whatwg.org/index-gb18030-ranges.txt (Date: %s) |
| 60 |
-- |
| 61 |
-- |
| 62 |
]] |
| 63 |
|
| 64 |
local rep, format, dump = string.rep, string.format, string.dump |
| 65 |
local insert, unpack, concat = table.insert, table.unpack, table.concat |
| 66 |
local io_open, os_execute = io.open, os.execute |
| 67 |
|
| 68 |
local curlcmd = "curl --silent --output %s https://encoding.spec.whatwg.org/%s" |
| 69 |
local function prepare_index (file) |
| 70 |
local file_path = file |
| 71 |
local handle = io_open(file, "rb") |
| 72 |
if handle then return handle end |
| 73 |
local ret = os_execute(curlcmd:format(file, file)) |
| 74 |
assert(ret == 0, "the curl command failed with: ".. ret) |
| 75 |
return assert(io_open(file, "rb")) |
| 76 |
end |
| 77 |
|
| 78 |
local index, date = { }, { } |
| 79 |
|
| 80 |
local indent = 2 |
| 81 |
local begin_enc = "%s[%q] = {" |
| 82 |
local end_enc = "%s} ," |
| 83 |
local num_item = "%s[%s] = %s ," |
| 84 |
local tab_item = "%s{ %6d , %s } ," |
| 85 |
local tab = rep(" ", indent) |
| 86 |
local tabtab = tab .. tab |
| 87 |
|
| 88 |
insert(index, "return {") |
| 89 |
for i, v in ipairs { { "index-big5.txt", "big5" }, |
| 90 |
{ "index-gb18030.txt", "gb18030" }, |
| 91 |
{ "index-gb18030-ranges.txt", "gb18030_ranges" } } do |
| 92 |
local file, encode = unpack(v) |
| 93 |
local handle = prepare_index(file) |
| 94 |
insert(index, begin_enc:format(tab, encode)) |
| 95 |
for line in handle:lines() do |
| 96 |
if not date[i] then |
| 97 |
local s = line:match("Date: (.+)$") |
| 98 |
if s then date[i] = s end |
| 99 |
end |
| 100 |
local pointer, code_point = line:match("^%s*(%d+)\t(0x%x+)") |
| 101 |
if pointer and code_point then |
| 102 |
insert(index, format(i == 3 and tab_item or num_item, tabtab, pointer, code_point)) |
| 103 |
end |
| 104 |
end |
| 105 |
insert(index, end_enc:format(tab)) |
| 106 |
handle:close() |
| 107 |
end |
| 108 |
insert(index, "}\n") |
| 109 |
|
| 110 |
local index = concat(index, "\n") |
| 111 |
|
| 112 |
local handle = io_open("ctex-zhconv-index.lua", "wb") |
| 113 |
handle:write(preamble:format(unpack(date)), index) |
| 114 |
handle:close() |