Code: Select all
-- Displays the current pattern's comments in the help window,
-- with clickable links to http sites and pattern files.
-- Author: Andrew Trevorrow (andrew@trevorrow.com)
local g = golly()
--------------------------------------------------------------------------------
function ModifyComments(comments)
comments = comments:gsub("\n", "<br>\n")
comments = "\n"..comments -- simplifies the following changes
comments = comments:gsub("\n#N ", "\nName: ", 1)
comments = comments:gsub("\n#O ", "\nAuthor: ", 1)
-- remove "#C " or "#D " or "# " from start of each line
-- (#D is used in .mcl files in Patterns/Generations/)
comments = comments:gsub("\n#[CD] ", "\n")
comments = comments:gsub("\n# ", "\n")
-- fix lines with only #C/#D/# (eg. Turmites/AlienCounter.rle)
comments = comments:gsub("\n#[CD]<br>", "\n<p>\n")
comments = comments:gsub("\n#<br>", "\n<p>\n")
-- replace space(s) at start of each line with non-breaking spaces
comments = comments:gsub("\n ", "\n ")
comments = comments:gsub("\n ", "\n ")
-- this makes tabular info less ugly (esp in osc stamp collection)
comments = comments:gsub(" ", "\n ")
-- escape % for later use as replacement string
comments = comments:gsub("%%", "%%%%")
-- go thru comments line by line to convert http links and pattern links
local newlines = {}
local nextline = comments:gmatch("(.-)\n")
while true do
local line = nextline()
if not line then break end
local prefix, link, suffix = line:match("^(.*)(http[^ <%),]+)(.+)$")
if link then
line = prefix.."<a href=\""..link.."\">"..link.."</a>"..suffix
else
prefix, link, suffix = line:match("^(.*)(Patterns/[^ <%)]+)(.+)$")
if link then
line = prefix.."<a href=\"open:"..link.."\">"..link.."</a>"..suffix
end
end
newlines[#newlines+1] = line
end
return table.concat(newlines)
end
--------------------------------------------------------------------------------
function Main()
local pathname = g.getpath()
if pathname == "" then g.exit("No current pattern.") end
local comments = g.getinfo()
if comments == "" then g.exit("No comments in current pattern.") end
comments = ModifyComments(comments)
-- extract the directory path and file name
local pathsep = g.getdir("app"):sub(-1)
local dirpath = pathname:sub(1, pathname:find(pathsep.."[^"..pathsep.."/]*$"))
local filename = pathname:sub(#dirpath+1)
-- create a temporary html file
local htmlname = g.getdir("temp")..filename..".html"
local f = io.open(htmlname, "w")
if f then
local htmldata =
[[
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Pattern Info for FILENAME</title>
<body bgcolor="#FFFFCE">
<p>COMMENTS</p>
</body>
</html>
]]
htmldata = htmldata:gsub("FILENAME", filename, 1)
htmldata = htmldata:gsub("COMMENTS", comments, 1)
f:write(htmldata)
f:close()
else
g.exit("Failed to create temporary html file!")
end
g.open(htmlname)
end
--------------------------------------------------------------------------------
Main()
Similarly, if you load the oscillator-stamp-collection and display its comments you'll find a link to http://www.argentum.freeserve.co.uk/lex_home.htm which no longer exists.
EDIT: I've fixed the above bad links for the next Golly release. Also corrected a few minor problems in the script.
EDIT #2: Modified the script so any Unicode characters will be displayed correctly (assuming the file is UTF-8 encoded).