|
| 1 | +#!/usr/bin/env lua |
| 2 | + |
| 3 | +-- fix-index-paths.lua |
| 4 | +-- Post-render script to fix paths in the generated site |
| 5 | +-- This script: |
| 6 | +-- 1. Copies docs/index.html to the site root and fixes relative paths |
| 7 | +-- 2. Copies markdown files from _site/docs back to docs directory |
| 8 | + |
| 9 | +-- Helper function to check if file exists |
| 10 | +local function file_exists(path) |
| 11 | + local f = io.open(path, "r") |
| 12 | + if f ~= nil then |
| 13 | + io.close(f) |
| 14 | + return true |
| 15 | + else |
| 16 | + return false |
| 17 | + end |
| 18 | +end |
| 19 | + |
| 20 | +-- Helper function to check if directory exists |
| 21 | +local function dir_exists(path) |
| 22 | + -- Try to open the directory as a file, which will fail |
| 23 | + -- Then try using a platform-agnostic approach |
| 24 | + local f = io.open(path .. "/.", "r") |
| 25 | + if f then |
| 26 | + io.close(f) |
| 27 | + return true |
| 28 | + end |
| 29 | + return false |
| 30 | +end |
| 31 | + |
| 32 | +-- Helper function to read file |
| 33 | +local function read_file(path) |
| 34 | + local f = io.open(path, "r") |
| 35 | + if not f then |
| 36 | + return nil |
| 37 | + end |
| 38 | + local content = f:read("*all") |
| 39 | + f:close() |
| 40 | + return content |
| 41 | +end |
| 42 | + |
| 43 | +-- Helper function to write file |
| 44 | +local function write_file(path, content) |
| 45 | + local f = io.open(path, "w") |
| 46 | + if not f then |
| 47 | + return false |
| 48 | + end |
| 49 | + f:write(content) |
| 50 | + f:close() |
| 51 | + return true |
| 52 | +end |
| 53 | + |
| 54 | +-- Helper function to copy file |
| 55 | +local function copy_file(src, dest) |
| 56 | + local content = read_file(src) |
| 57 | + if content then |
| 58 | + return write_file(dest, content) |
| 59 | + end |
| 60 | + return false |
| 61 | +end |
| 62 | + |
| 63 | +-- Helper function to list files in directory |
| 64 | +local function list_files(dir, pattern) |
| 65 | + local files = {} |
| 66 | + local command |
| 67 | + |
| 68 | + -- Detect OS and use appropriate command |
| 69 | + if package.config:sub(1, 1) == "\\" then |
| 70 | + -- Windows |
| 71 | + command = 'dir /b "' .. dir .. '" 2>nul' |
| 72 | + else |
| 73 | + -- Unix-like (Linux, macOS) |
| 74 | + command = 'ls "' .. dir .. '" 2>/dev/null' |
| 75 | + end |
| 76 | + |
| 77 | + local handle = io.popen(command) |
| 78 | + if handle then |
| 79 | + for file in handle:lines() do |
| 80 | + if pattern and file:match(pattern) then |
| 81 | + table.insert(files, file) |
| 82 | + elseif not pattern then |
| 83 | + table.insert(files, file) |
| 84 | + end |
| 85 | + end |
| 86 | + handle:close() |
| 87 | + end |
| 88 | + |
| 89 | + return files |
| 90 | +end |
| 91 | + |
| 92 | +-- Main function |
| 93 | +local function main() |
| 94 | + -- Define paths relative to project root |
| 95 | + local site_dir = "_site" |
| 96 | + local docs_dir = "docs" |
| 97 | + local source_index = site_dir .. "/docs/index.html" |
| 98 | + local dest_index = site_dir .. "/index.html" |
| 99 | + |
| 100 | + -- Part 1: Copy and fix index.html |
| 101 | + if file_exists(source_index) then |
| 102 | + local content = read_file(source_index) |
| 103 | + if content then |
| 104 | + -- Fix relative paths |
| 105 | + content = content:gsub('href="../docs/', 'href="docs/') |
| 106 | + content = content:gsub('src="../site_libs/', 'src="site_libs/') |
| 107 | + |
| 108 | + if write_file(dest_index, content) then |
| 109 | + print("✓ Fixed paths in root index.html") |
| 110 | + else |
| 111 | + print("⚠️ Failed to write fixed index.html") |
| 112 | + end |
| 113 | + else |
| 114 | + print("⚠️ Failed to read " .. source_index) |
| 115 | + end |
| 116 | + else |
| 117 | + print("⚠️ No _site directory found - using default homepage") |
| 118 | + end |
| 119 | + |
| 120 | + -- Part 2: Copy markdown files back to docs directory |
| 121 | + local site_docs_dir = site_dir .. "/docs" |
| 122 | + if dir_exists(site_docs_dir) then |
| 123 | + local md_files = list_files(site_docs_dir, "%.md$") |
| 124 | + local copied = false |
| 125 | + |
| 126 | + for _, file in ipairs(md_files) do |
| 127 | + local src = site_docs_dir .. "/" .. file |
| 128 | + local dest = docs_dir .. "/" .. file |
| 129 | + if copy_file(src, dest) then |
| 130 | + copied = true |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + if copied then |
| 135 | + print("✓ Markdown files copied to docs/") |
| 136 | + else |
| 137 | + print("⚠️ No markdown files found") |
| 138 | + end |
| 139 | + end |
| 140 | +end |
| 141 | + |
| 142 | +-- Run the script |
| 143 | +main() |
0 commit comments