diff --git a/macros/roadsheet/README.md b/macros/roadsheet/README.md new file mode 100644 index 0000000..8a0f3cd --- /dev/null +++ b/macros/roadsheet/README.md @@ -0,0 +1,26 @@ +# roadsheet + +Printable contact/road sheet HTML report for caches in the current filter. + +This is a port of the GSAK RoadSheet macro, originally written by +geo-mario. Porting was aided by use of Google Gemini AI. + +## What it needs + +- OpenSAK with macro API version 3 or newer +- Permissions: open + +Opens .html page in your default browser upon completion. + +## How to use it + +1. Set a filter to show (only) the caches to be printed to the roadsheet. +2. Run the macro from **Macros → Macro manager…**. + +## Example + +[Sample roadsheet](file:///examples/roadsheet-example.png) + +## Changes + +- 1.0 — first version (ported from GSAK RoadSheet macro by geo-mario) diff --git a/macros/roadsheet/examples/roadsheet-example.png b/macros/roadsheet/examples/roadsheet-example.png new file mode 100644 index 0000000..b18d697 Binary files /dev/null and b/macros/roadsheet/examples/roadsheet-example.png differ diff --git a/macros/roadsheet/roadsheet.lua b/macros/roadsheet/roadsheet.lua new file mode 100644 index 0000000..1567c4d --- /dev/null +++ b/macros/roadsheet/roadsheet.lua @@ -0,0 +1,342 @@ +-- OpenSAK Macro -- +-- Name: RoadSheet +-- Author: OokOok +-- Version: 1.0 +-- Requires-API: 3 +-- Description: Printable contact/road sheet HTML report for caches in the current filter. +-- Permissions: open + +-- Ported from geo-mario's original RoadSheet macro originally written for GSAK. + +local total = opensak.count() +if total == 0 then + opensak.message("No caches in the current filter.", "RoadSheet") + return +end + +-- Helper: escape HTML special characters +local function esc(s) + if s == nil then return "" end + return tostring(s) + :gsub("&", "&") + :gsub("<", "<") + :gsub(">", ">") + :gsub('"', """) +end + +-- Helper: format decimal degrees to Geocaching DD° MM.MMM' (equivalent to GSAK FormatMinutes) +local function format_dm(lat, lon) + if not lat or not lon then return "" end + local function fmt(deg, pos_hemi, neg_hemi) + local hemi = deg >= 0 and pos_hemi or neg_hemi + local abs_deg = math.abs(deg) + local d = math.floor(abs_deg) + local m = (abs_deg - d) * 60 + return string.format("%s %02d° %06.3f'", hemi, d, m) + end + return fmt(lat, "N", "S") .. "
" .. fmt(lon, "E", "W") +end + +-- Helper: build the 2x2 Last-4-Logs color grid (green = Found, red = DNF, yellow = Other, gray = Empty) +local function build_l4_grid(code) + local ok, logs = pcall(opensak.logs, code) + logs = (ok and type(logs) == "table") and logs or {} + + local cells = {} + for i = 1, 4 do + local log = logs[i] + if log then + local ltype = tostring(log.type or log.log_type or "") + local ldate = tostring(log.date or log.log_date or "") + if ltype == "Found it" then + cells[i] = string.format('y', esc(ltype), esc(ldate)) + elseif ltype == "Didn't find it" then + cells[i] = string.format('n', esc(ltype), esc(ldate)) + else + cells[i] = string.format('o', esc(ltype), esc(ldate)) + end + else + cells[i] = 'x' + end + end + + return string.format( + '%s%s%s%s
', + cells[1], cells[2], cells[3], cells[4] + ) +end + +-- Helper: normalize attribute name to Geocaching filename slug +local function attr_slug(raw) + local s = tostring(raw or ""):lower() + -- Common Geocaching attribute filename mappings if full names are returned + local map = { + ["dogs"] = "dogs", + ["dogs allowed"] = "dogs", + ["access or parking fee"] = "fee", + ["climbing gear required"] = "rappelling", + ["boat required"] = "boat", + ["scuba gear required"] = "scuba", + ["recommended for kids"] = "kids", + ["takes less than an hour"] = "onehour", + ["scenic view"] = "scenic", + ["significant hike"] = "hiking", + ["difficult climbing"] = "climbing", + ["may require wading"] = "wading", + ["may require swimming"] = "swimming", + ["may require snowshoes"] = "snowshoes", + ["may require cross country skis"] = "skiis", + ["available at all times"] = "available", + ["available 24/7"] = "available", + ["recommended at night"] = "night", + ["available during winter"] = "winter", + ["poison plants"] = "poisonoak", + ["dangerous animals"] = "dangerousanimals", + ["ticks"] = "ticks", + ["abandoned mine"] = "mine", + ["ticks"] = "ticks", + ["cliff / falling rocks"] = "cliff", + ["hunting"] = "hunting", + ["dangerous area"] = "danger", + ["wheelchair accessible"] = "wheelchair", + ["parking nearby"] = "parking", + ["public transportation nearby"] = "public", + ["drinking water nearby"] = "water", + ["public restrooms nearby"] = "restrooms", + ["telephone nearby"] = "phone", + ["picnic tables nearby"] = "picnic", + ["camping nearby"] = "camping", + ["bicycles"] = "bicycles", + ["motorcycles"] = "motorcycles", + ["quads"] = "quads", + ["off-road vehicles / jeeps"] = "jeeps", + ["snowmobiles"] = "snowmobiles", + ["horses"] = "horses", + ["campfires"] = "campfires", + ["thorns"] = "thorns", + ["stealth required"] = "stealth", + ["stroller accessible"] = "stroller", + ["needs maintenance"] = "firstaid", + ["livestock nearby"] = "cow", + ["flashlight required"] = "flashlight", + ["lost and found tour"] = "landf", + ["trucks / rvs"] = "rv", + ["field puzzle"] = "field_puzzle", + ["uv light required"] = "uv", + ["snowshoes"] = "snowshoes", + ["cross country skis"] = "skiis", + ["special tool required"] = "s-tool", + ["night cache"] = "nightcache", + ["park and grab"] = "parkngrab", + ["abandoned structure"] = "abandonedbuilding", + ["short hike (<1 km)"] = "hike_short", + ["medium hike (1-10 km)"] = "hike_med", + ["long hike (> 10 km)"] = "hike_long", + ["fuel nearby"] = "fuel", + ["food nearby"] = "food", + ["wireless beacon"] = "wirelessbeacon", + ["partnership cache"] = "partnership", + ["seasonal access"] = "seasonal", + ["recommended for tourists"] = "touristok", + ["tree climbing required"] = "treeclimbing", + ["yard (private residence)"] = "frontyard", + ["teamwork required"] = "teamwork", + ["geotour"] = "geotour", + ["bonus cache"] = "bonuscache", + ["power trail"] = "powertrail", + ["challenge cache"] = "challengecache", + ["geocaching.com solution checker"] = "hqsolutionchecker" + } + if map[s] then + return map[s] + end + -- If already a slug or unknown name, clean spaces/slashes to match filename conventions + s = s:gsub("%.png$", ""):gsub("%-yes$", ""):gsub("%-no$", "") + s = s:gsub("[^a-z0-9_%-]", "") + return s +end + +-- Helper: build attributes icons (-yes.png / -no.png / .png) +local function build_attributes(code) + local ok, attrs = pcall(opensak.attributes, code) + if not ok or type(attrs) ~= "table" or #attrs == 0 then + return "" + end + + local base_url = "https://geocaching.com/images/attributes/" + local items = {} + + for _, a in ipairs(attrs) do + local raw_name = tostring(a.slug or a.icon or a.name or a.attribute or a.id or "attribute-blank") + local display_name = tostring(a.name or a.attribute or a.id or "Attr") + local inc = (a.inc == true or a.inc == 1 or a.positive == true or a.yes == true) + local slug = attr_slug(raw_name) + local suffix = inc and "-yes" or "-no" + local label = (inc and "+" or "-") .. display_name + + local primary_src = base_url .. slug .. suffix .. ".png" + local fallback_src = base_url .. slug .. ".png" + + -- Falls back to .png if -yes/-no fails, then to text if neither exists + local onerror_js = string.format( + "if(!this.dataset.tried){this.dataset.tried=1;this.src='%s';}else{this.outerHTML='%s';}", + esc(fallback_src), + inc and "attr-yes" or "attr-no", + esc(label) + ) + + items[#items + 1] = string.format( + '%s', + esc(primary_src), + esc(label), + esc(label), + onerror_js + ) + end + return table.concat(items, "") +end + +-- Build HTML document +local rows = {} +local idx = 0 + +local fields = { + "code", "name", "type", "container", "lat", "lon", "corrected", + "difficulty", "terrain", "owner", "placed_by", "last_log_date", + "trackable_count", "hint", "url" +} + +for c in opensak.caches{ fields = fields } do + idx = idx + 1 + if idx % 10 == 0 or idx == 1 then + opensak.progress(string.format("Processing cache %d / %d...", idx, total)) + end + + local lat = (c.corrected and c.corrected.lat) or c.lat + local lon = (c.corrected and c.corrected.lon) or c.lon + local coords_html = format_dm(lat, lon) + if c.corrected then + coords_html = "* " .. coords_html + end + + local url = (c.url and c.url ~= "") and c.url or ("https://coord.info" .. tostring(c.code)) + local code_link = string.format('%s', esc(url), esc(c.code)) + + local owner = c.owner or c.placed_by or "" + local last_date = c.last_log_date and tostring(c.last_log_date):sub(1, 10) or "" + local dt_str = string.format("D: %s
T: %s", tostring(c.difficulty or "?"), tostring(c.terrain or "?")) + + local l4_html = build_l4_grid(c.code) + local attr_html = build_attributes(c.code) + + local hint_parts = {} + if c.trackable_count and c.trackable_count > 0 then + hint_parts[#hint_parts + 1] = string.format('[TB:%d]', c.trackable_count) + end + if c.hint and c.hint ~= "" then + local clean_hint = tostring(c.hint):gsub("\r\n", ", "):gsub("\n", ", ") + hint_parts[#hint_parts + 1] = esc(clean_hint) + end + local hint_html = table.concat(hint_parts, " ") + + local short_type = "" + if c.type:match("^([^ ]*)") == "Cache" then + short_type = "CITO" + else + short_type = c.type:match("^([^ ]*)") + end + + local row_html = string.format( + '' + .. '%s' + .. '%s' + .. '%s' + .. '%s' + .. '%s' + .. '%s' + .. '%s' + .. '%s' + .. '%s' + .. '%s' + .. '%s' + .. '', + code_link, + esc(c.name), + esc(short_type), + esc(c.container), + l4_html, + esc(owner), + esc(last_date), + coords_html, + dt_str, + attr_html, + hint_html + ) + rows[#rows + 1] = row_html +end + +local title = string.format("RoadSheet - Number of caches in current filter: %d", total) + +local html = string.format([[ + + + +%s + + + +

%s

+ + + + + + + + + + + + + + + + + +%s + +
CodeNameTypeSizeL4OwnerLast LogCoordinatesD / TAttributesHint
+ + +]], esc(title), esc(title), table.concat(rows, "\n")) + + +local out_path = opensak.temp_dir() .. "/roadsheet.html" +opensak.write_text(out_path, html) +opensak.open(out_path) \ No newline at end of file