Last active
April 16, 2018 11:07
-
-
Save luxuia/2d892fcffe2e7d401e697de743f62cb6 to your computer and use it in GitHub Desktop.
xml_diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env lua | |
--1.a git config diff.xml_diff.textconv xml_diff.lua | |
--1.b editor file: .git/config | |
---- [diff "xml_diff"] | |
---- textconv = xml_diff.lua | |
--2. editor file: .gitattributes | |
---- *.xml diff=xml_diff | |
--3. 把这个文件放在可执行路径下,比如当前指令运行目录 | |
local args = {...} | |
local filepath = args[1] | |
local txt = io.open(filepath, "r"):read("*a") | |
-- 不是临时文件 | |
local print_first_line | |
if string.find(filepath, "design") then | |
-- 用于打印表头 | |
print_first_line = true | |
end | |
local XML_DECODE = { | |
['amp'] = '&', | |
['lt'] = '<', | |
['gt'] = '>', | |
['quot'] = '"', | |
['apos'] = "'", | |
['nbsp'] = ' ', | |
['copy'] = '©', | |
['reg'] = '®', | |
['#10'] = ' ', --换行 | |
['#45'] = '-', | |
['#13'] = ' ', --换行 | |
} | |
local function decode_text(text) | |
return string.gsub(text, '&([%w%d#]+);', XML_DECODE) | |
end | |
for name, sheet in string.gmatch(txt, "<Worksheet ss:Name=\"(.-)\"(.-)</Worksheet>") do | |
-- 打印表名 | |
print(string.format("----------- Table: %s -------------", name)) | |
local lines = {} | |
local count = 1 | |
for line in string.gmatch(sheet, "<Row(.-)/Row>") do | |
-- 打印第几行 | |
local cells = {string.format("Row:%d", count)} | |
for c in string.gmatch(line, "<Data.->(.-)</Data>") do | |
-- 比较第几列,可以选择性过滤掉几列 | |
table.insert(cells, c) | |
end | |
lines[count] = table.concat(cells, "\t") | |
count = count + 1 | |
end | |
if print_first_line then | |
table.remove(lines, 2) | |
table.remove(lines, 2) | |
print_first_line = nil | |
else | |
table.remove(lines, 1) | |
table.remove(lines, 1) | |
table.remove(lines, 1) | |
end | |
local result = table.concat(lines, "\n") | |
result = decode_text(result) | |
print(result) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment