scripts/markdown.lua: Added support for 'topicmeeting' screens. Added option to provide own filename. Added option to overwrite previous data.

develop
Mchl 2014-09-28 18:52:49 +02:00
parent febc9d08b4
commit 81d1a1c20e
1 changed files with 65 additions and 14 deletions

@ -18,21 +18,33 @@ description:
document containing the text from multiple screens (eg: text screens document containing the text from multiple screens (eg: text screens
from several dwarves, or text screens from multiple artifacts/items, from several dwarves, or text screens from multiple artifacts/items,
or some combination). or some combination).
usage:
markdown [/n] [filename]
/n - overwrites contents of output file
filename - if provided, the data will be saved in md_filename.md instead
of default md_export.md
known screens: known screens:
The screens which have been tested and known to function properly with The screens which have been tested and known to function properly with
this script are: this script are:
1: dwarf/unit 'thoughts' screen 1: dwarf/unit 'thoughts' screen
2: item/art 'description' screen 2: item/art 'description' screen
3: individual 'historical item/figure' screens 3: individual 'historical item/figure' screens
4: announement screen 4: manual
4: announements screen
5: combat reports screen 5: combat reports screen
6: latest news (when meeting with liaison)
There may be other screens to which the script applies. It should be There may be other screens to which the script applies. It should be
safe to attempt running the script with any screen active, with an safe to attempt running the script with any screen active, with an
error message to inform you when the selected screen is not appropriate error message to inform you when the selected screen is not appropriate
for this script. for this script.
target file: target file:
The target file's name is 'markdownexport.txt'. A remider to this effect The default target file's name is 'md_export.md'. A remider to this effect
will be displayed if the script is successful. will be displayed if the script is successful.
character encoding: character encoding:
The text will likely be using system-default encoding, and as such The text will likely be using system-default encoding, and as such
will likely NOT display special characters (eg:é,õ,ç) correctly. To will likely NOT display special characters (eg:é,õ,ç) correctly. To
@ -46,10 +58,29 @@ character encoding:
]]) ]])
return return
end end
local writemode = 'a'
-- check if we want to append to an existing file (default) or overwrite previous contents
if args[1] == '/n' then
writemode = 'w'
table.remove(args, 1)
end
local filename
if args[1] ~= nil then
filename = 'md_' .. table.remove(args, 1) .. '.md'
else
filename = 'md_export.md'
end
local utils = require 'utils' local utils = require 'utils'
local gui = require 'gui' local gui = require 'gui'
local dialog = require 'gui.dialogs' local dialog = require 'gui.dialogs'
local filename = 'markdownexport.txt'
local scrn = dfhack.gui.getCurViewscreen() local scrn = dfhack.gui.getCurViewscreen()
local flerb = dfhack.gui.getFocusString(scrn) local flerb = dfhack.gui.getFocusString(scrn)
@ -69,6 +100,16 @@ local months = {
[12] = 'Obsidian', [12] = 'Obsidian',
} }
local function getFileHandle()
return io.open(filename, writemode)
end
local function closeFileHandle(handle)
handle:write('\n***\n\n')
handle:close()
print ('Data exported to "' .. filename .. '"')
end
local function reformat(strin) local function reformat(strin)
local strout = strin local strout = strin
@ -137,7 +178,7 @@ if flerb == 'textviewer' then
if lines ~= nil then if lines ~= nil then
local log = io.open(filename, 'a') local log = getFileHandle()
log:write('### ' .. scrn.title .. '\n') log:write('### ' .. scrn.title .. '\n')
print('Exporting ' .. scrn.title .. '\n') print('Exporting ' .. scrn.title .. '\n')
@ -147,35 +188,45 @@ if flerb == 'textviewer' then
-- debug output -- debug output
-- print(x.value) -- print(x.value)
end end
closeFileHandle(log)
log:write('\n***\n\n')
log:close()
end end
print ('Data exported to "' .. filename .. '"')
elseif flerb == 'announcelist' then elseif flerb == 'announcelist' then
local lines = scrn.reports local lines = scrn.reports
if lines ~= nil then if lines ~= nil then
local log = io.open(filename, 'a') local log = getFileHandle()
local lastTime = "" local lastTime = ""
for n,x in ipairs(lines) do for n,x in ipairs(lines) do
local currentTime = formattime(x.year, x.time) local currentTime = formattime(x.year, x.time)
if (currentTime ~= lastTime) then if (currentTime ~= lastTime) then
lastTime = currentTime lastTime = currentTime
log:write('\n***\n\n') log:write('\n***\n\n')
log:write('## ' .. currentTime .. '\n') log:write('## ' .. currentTime .. '\n')
end end
-- debug output -- debug output
-- print(x.text) -- print(x.text)
log:write(x.text .. '\n') log:write(x.text .. '\n')
end end
closeFileHandle(log)
log:close() end
elseif flerb == 'topicmeeting' then
local lines = scrn.text
if lines ~= nil then
local log = getFileHandle()
for n,x in ipairs(lines) do
-- debug output
-- print(x.value)
log:write(x.value .. '\n')
end
closeFileHandle(log)
end end
print ('Data exported to "' .. filename .. '"')
else else
print 'This is not a textview nor an announcelist screen. Can\'t export data, sorry.' print 'This is not a textview, announcelist or topicmeeting screen. Can\'t export data, sorry.'
end end