2014-09-28 15:50:50 -06:00
|
|
|
-- Save a copy of a text screen in markdown (for reddit among others). Use 'markdown help' for more details.
|
2014-09-26 17:37:29 -06:00
|
|
|
-- This is a derivatiwe work based upon scripts/forum-dwarves.lua by Caldfir and expwnent
|
|
|
|
-- Adapted for markdown by Mchl https://github.com/Mchl
|
|
|
|
|
|
|
|
local args = {...}
|
|
|
|
|
2015-02-14 20:53:06 -07:00
|
|
|
if args[1] == 'help' then
|
2014-09-26 17:37:29 -06:00
|
|
|
print([[
|
|
|
|
description:
|
2015-02-14 20:53:06 -07:00
|
|
|
This script will attempt to read the current df-screen, and if it is a
|
2014-09-26 17:37:29 -06:00
|
|
|
text-viewscreen (such as the dwarf 'thoughts' screen or an item / creature
|
2015-02-14 20:53:06 -07:00
|
|
|
'description') or an announcement list screen (such as announcements and
|
|
|
|
combat reports) then append a marked-down version of this text to the
|
2014-09-26 17:37:29 -06:00
|
|
|
target file (for easy pasting on reddit for example).
|
2015-02-14 20:53:06 -07:00
|
|
|
Previous entries in the file are not overwritten, so you
|
|
|
|
may use the 'markdown' command multiple times to create a single
|
|
|
|
document containing the text from multiple screens (eg: text screens
|
|
|
|
from several dwarves, or text screens from multiple artifacts/items,
|
|
|
|
or some combination).
|
|
|
|
|
2014-09-28 10:52:49 -06:00
|
|
|
usage:
|
|
|
|
markdown [/n] [filename]
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-28 10:52:49 -06:00
|
|
|
/n - overwrites contents of output file
|
|
|
|
filename - if provided, the data will be saved in md_filename.md instead
|
2015-02-14 20:53:06 -07:00
|
|
|
of default md_export.md
|
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
known screens:
|
2015-02-14 20:53:06 -07:00
|
|
|
The screens which have been tested and known to function properly with
|
2014-09-26 17:37:29 -06:00
|
|
|
this script are:
|
|
|
|
1: dwarf/unit 'thoughts' screen
|
|
|
|
2: item/art 'description' screen
|
|
|
|
3: individual 'historical item/figure' screens
|
2015-02-14 20:53:06 -07:00
|
|
|
4: manual
|
2014-09-28 10:52:49 -06:00
|
|
|
4: announements screen
|
2014-09-27 07:24:28 -06:00
|
|
|
5: combat reports screen
|
2014-09-28 10:52:49 -06:00
|
|
|
6: latest news (when meeting with liaison)
|
2015-02-14 20:53:06 -07:00
|
|
|
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
|
2014-09-26 17:37:29 -06:00
|
|
|
error message to inform you when the selected screen is not appropriate
|
|
|
|
for this script.
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
target file:
|
2014-09-28 10:52:49 -06:00
|
|
|
The default target file's name is 'md_export.md'. A remider to this effect
|
2015-02-14 20:53:06 -07:00
|
|
|
will be displayed if the script is successful.
|
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
character encoding:
|
2015-02-14 20:53:06 -07:00
|
|
|
The text will likely be using system-default encoding, and as such
|
2015-04-06 09:25:30 -06:00
|
|
|
will likely NOT display special characters (eg:é,õ,ç) correctly. To
|
2015-02-14 20:53:06 -07:00
|
|
|
fix this, you need to modify the character set that you are reading
|
|
|
|
the document with. 'Notepad++' is a freely available program which
|
2014-09-26 17:37:29 -06:00
|
|
|
can do this using the following steps:
|
|
|
|
1: open the document in Notepad++
|
2015-02-14 20:53:06 -07:00
|
|
|
2: in the menu-bar, select
|
2014-09-26 17:37:29 -06:00
|
|
|
Encoding->Character Sets->Western European->OEM-US
|
|
|
|
3: copy the text normally to wherever you want to use it
|
|
|
|
]])
|
|
|
|
return
|
|
|
|
end
|
2014-09-28 10:52:49 -06:00
|
|
|
|
|
|
|
local writemode = 'a'
|
|
|
|
|
2015-02-14 20:53:06 -07:00
|
|
|
-- check if we want to append to an existing file (default) or overwrite previous contents
|
2014-09-28 10:52:49 -06:00
|
|
|
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'
|
2015-02-14 20:53:06 -07:00
|
|
|
else
|
2014-09-28 10:52:49 -06:00
|
|
|
filename = 'md_export.md'
|
|
|
|
end
|
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
local utils = require 'utils'
|
|
|
|
local gui = require 'gui'
|
|
|
|
local dialog = require 'gui.dialogs'
|
2014-09-28 10:52:49 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
|
|
|
|
local scrn = dfhack.gui.getCurViewscreen()
|
|
|
|
local flerb = dfhack.gui.getFocusString(scrn)
|
|
|
|
|
2014-09-27 06:46:38 -06:00
|
|
|
local months = {
|
|
|
|
[1] = 'Granite',
|
|
|
|
[2] = 'Slate',
|
|
|
|
[3] = 'Felsite',
|
|
|
|
[4] = 'Hematite',
|
|
|
|
[5] = 'Malachite',
|
|
|
|
[6] = 'Galena',
|
|
|
|
[7] = 'Limestone',
|
|
|
|
[8] = 'Sandstone',
|
|
|
|
[9] = 'Timber',
|
|
|
|
[10] = 'Moonstone',
|
|
|
|
[11] = 'Opal',
|
|
|
|
[12] = 'Obsidian',
|
|
|
|
}
|
2014-09-26 17:37:29 -06:00
|
|
|
|
2014-09-28 10:52:49 -06:00
|
|
|
local function getFileHandle()
|
|
|
|
return io.open(filename, writemode)
|
|
|
|
end
|
|
|
|
|
2015-02-14 20:53:06 -07:00
|
|
|
local function closeFileHandle(handle)
|
2014-09-28 10:52:49 -06:00
|
|
|
handle:write('\n***\n\n')
|
|
|
|
handle:close()
|
|
|
|
print ('Data exported to "' .. filename .. '"')
|
|
|
|
end
|
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
local function reformat(strin)
|
|
|
|
local strout = strin
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
-- [P] tags seem to indicate a new paragraph
|
|
|
|
local newline_idx = string.find(strout, '[P]', 1, true)
|
|
|
|
while newline_idx ~= nil do
|
|
|
|
strout = string.sub(strout, 1, newline_idx - 1) .. '\n***\n\n' .. string.sub(strout, newline_idx + 3)
|
|
|
|
newline_idx = string.find(strout, '[P]', 1, true)
|
|
|
|
end
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
-- [R] tags seem to indicate a new 'section'. Let's mark it with a horizontal line.
|
|
|
|
newline_idx = string.find(strout, '[R]', 1, true)
|
|
|
|
while newline_idx ~= nil do
|
|
|
|
strout = string.sub(strout, 1, newline_idx - 1) .. '\n***\n\n' .. string.sub(strout,newline_idx + 3)
|
|
|
|
newline_idx = string.find(strout, '[R]', 1, true)
|
|
|
|
end
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
-- No idea what [B] tags might indicate. Just removing them seems to work fine
|
|
|
|
newline_idx = string.find(strout, '[B]', 1, true)
|
|
|
|
while newline_idx ~= nil do
|
|
|
|
strout = string.sub(strout, 1, newline_idx - 1) .. string.sub(strout,newline_idx + 3)
|
|
|
|
newline_idx = string.find(strout, '[B]', 1, true)
|
|
|
|
end
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
-- Reddit doesn't support custom colors in markdown. We need to remove all color information :(
|
|
|
|
local color_idx = string.find(strout, '[C:', 1, true)
|
2015-02-14 20:53:06 -07:00
|
|
|
while color_idx ~= nil do
|
2014-09-26 17:37:29 -06:00
|
|
|
strout = string.sub(strout, 1, color_idx - 1) .. string.sub(strout, color_idx + 9)
|
|
|
|
color_idx = string.find(strout, '[C:', 1, true)
|
|
|
|
end
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
return strout
|
|
|
|
end
|
|
|
|
|
2014-09-27 06:46:38 -06:00
|
|
|
local function formattime(year, ticks)
|
|
|
|
-- Dwarf Mode month is 33600 ticks long
|
|
|
|
local month = math.floor(ticks / 33600)
|
|
|
|
local dayRemainder = ticks - month * 33600
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-27 06:46:38 -06:00
|
|
|
-- Dwarf Mode day is 1200 ticks long
|
|
|
|
local day = math.floor(dayRemainder / 1200)
|
|
|
|
local timeRemainder = dayRemainder - day * 1200
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-27 06:46:38 -06:00
|
|
|
-- Assuming a 24h day each Dwarf Mode tick corresponds to 72 seconds
|
|
|
|
local seconds = timeRemainder * 72
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-27 06:46:38 -06:00
|
|
|
local H = string.format("%02.f", math.floor(seconds / 3600));
|
|
|
|
local m = string.format("%02.f", math.floor(seconds / 60 - (H * 60)));
|
|
|
|
local i = string.format("%02.f", math.floor(seconds - H * 3600 - m * 60));
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-27 06:46:38 -06:00
|
|
|
day = day + 1
|
|
|
|
if (day == 1 or day == 21) then
|
|
|
|
day = day .. 'st'
|
|
|
|
elseif (day == 2 or day == 22) then
|
|
|
|
day = day .. 'nd'
|
|
|
|
else
|
|
|
|
day = day .. 'th'
|
|
|
|
end
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-27 06:46:38 -06:00
|
|
|
return (day .. " " .. months[month + 1] .. " " .. year .. " " .. H .. ":" .. m..":" .. i)
|
|
|
|
end
|
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
if flerb == 'textviewer' then
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
local lines = scrn.src_text
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
if lines ~= nil then
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-28 10:52:49 -06:00
|
|
|
local log = getFileHandle()
|
2014-09-26 17:37:29 -06:00
|
|
|
log:write('### ' .. scrn.title .. '\n')
|
|
|
|
|
|
|
|
print('Exporting ' .. scrn.title .. '\n')
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-26 17:37:29 -06:00
|
|
|
for n,x in ipairs(lines) do
|
|
|
|
log:write(reformat(x.value).." ")
|
2015-02-14 20:53:06 -07:00
|
|
|
-- debug output
|
|
|
|
-- print(x.value)
|
2014-09-26 17:37:29 -06:00
|
|
|
end
|
2014-09-28 10:52:49 -06:00
|
|
|
closeFileHandle(log)
|
2015-02-14 20:53:06 -07:00
|
|
|
end
|
|
|
|
|
2014-09-27 06:46:38 -06:00
|
|
|
elseif flerb == 'announcelist' then
|
|
|
|
|
|
|
|
local lines = scrn.reports
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-27 06:46:38 -06:00
|
|
|
if lines ~= nil then
|
2014-09-28 10:52:49 -06:00
|
|
|
local log = getFileHandle()
|
2014-09-27 06:46:38 -06:00
|
|
|
local lastTime = ""
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-27 06:46:38 -06:00
|
|
|
for n,x in ipairs(lines) do
|
|
|
|
local currentTime = formattime(x.year, x.time)
|
|
|
|
if (currentTime ~= lastTime) then
|
|
|
|
lastTime = currentTime
|
2014-09-28 10:52:49 -06:00
|
|
|
log:write('\n***\n\n')
|
2014-09-27 06:46:38 -06:00
|
|
|
log:write('## ' .. currentTime .. '\n')
|
|
|
|
end
|
2015-02-14 20:53:06 -07:00
|
|
|
-- debug output
|
2014-09-27 06:46:38 -06:00
|
|
|
-- print(x.text)
|
|
|
|
log:write(x.text .. '\n')
|
|
|
|
end
|
2014-09-28 10:52:49 -06:00
|
|
|
closeFileHandle(log)
|
|
|
|
end
|
2015-02-14 20:53:06 -07:00
|
|
|
|
|
|
|
|
2014-09-28 10:52:49 -06:00
|
|
|
elseif flerb == 'topicmeeting' then
|
|
|
|
local lines = scrn.text
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-28 10:52:49 -06:00
|
|
|
if lines ~= nil then
|
|
|
|
local log = getFileHandle()
|
2015-02-14 20:53:06 -07:00
|
|
|
|
|
|
|
for n,x in ipairs(lines) do
|
|
|
|
-- debug output
|
2014-09-28 10:52:49 -06:00
|
|
|
-- print(x.value)
|
|
|
|
log:write(x.value .. '\n')
|
|
|
|
end
|
|
|
|
closeFileHandle(log)
|
2014-09-27 06:46:38 -06:00
|
|
|
end
|
2014-09-26 17:37:29 -06:00
|
|
|
else
|
2014-09-28 10:52:49 -06:00
|
|
|
print 'This is not a textview, announcelist or topicmeeting screen. Can\'t export data, sorry.'
|
2014-09-26 17:37:29 -06:00
|
|
|
end
|