-- html.lua -- Formatting data with HTML -- Copyright (c) 2020 Joshua 'joshuas3' Stockin -- local lyaml = require("lyaml") local utils = require("utils") local git = require("git_commands") local request = require("request") local tabulate = require("tabulate") local create_html_template = function() local t = {} t.title = "" t.meta_tags = {} t.body = "" return t end local tree = function(repo, repo_dir, branch, path) local t = create_html_template() if path ~= "" then -- make sure path exists local path_tree = git.list_tree(repo_dir, branch.name, string.sub(path, 1, path:len() - 1)) if #path_tree.dirs == 0 then -- no path found return nil end end -- Header with repository description and navigation links t.body = t.body..string.format([[

%s / %s

]], repo.name, repo.name, repo.name, branch.name, branch.name) t.body = t.body.."

"..repo.description.."

" local navlinks_list = { "Download", "Refs", "Commit Log", "Files" } for _, special in pairs(repo.specialfiles) do local split = string.split(special, " ") table.insert(navlinks_list, string.format([[%s]], repo.name, branch.name, split[2], split[1])) end t.body = t.body.."

"..table.concat(navlinks_list, " | ").."

" -- Latest Commit table t.body = t.body.."

Latest Commit

" local commits_table_data = {} commits_table_data.class = "log" commits_table_data.headers = { {"timestamp", "Time"}, {"shorthash", "Hash"}, {"subject", "Subject"}, {"author", "Author"}, {"changed_files", [[#]]}, {"changed_plus", [[(+)]]}, {"changed_minus", [[(-)]]}, {"gpggood", [[GPG?]]} } commits_table_data.rows = {} local commits_head = git.log(repo_dir, branch.name, path, 1, 0, true) for _, commit in pairs(commits_head) do table.insert(commits_table_data.rows, { utils.iso8601(commit.timestamp), string.format([[%s]], repo.name, commit.hash, commit.shorthash), utils.html_sanitize(commit.subject), string.format([[%s]], commit.email, utils.html_sanitize(commit.author)), commit.diff.num, commit.diff.plus, commit.diff.minus, commit.gpggood }) end t.body = t.body..tabulate(commits_table_data) -- Tree/files table t.body = t.body.."

Tree" if path == "" then t.body = t.body.."

" else -- build path with hyperlinks for section header local split = string.split(path, "/") table.remove(split, #split) local base = "/"..repo.name.."/tree/"..branch.name t.body = t.body..string.format([[ @ %s]], base, repo.name) local build = "" for _, part in pairs(split) do build = build.."/"..part t.body = t.body..string.format([[ / %s]], base, build, part) end t.body = t.body.."" end local files_table_data = {} files_table_data.class = "files" files_table_data.headers = { {"object", "Object"}, {"subject", "Latest Commit Subject"}, {"timestamp", "Time"}, {"shorthash", "Hash"}} files_table_data.rows = {} local files = git.list_tree(repo_dir, branch.name, path) local file_icon = [[]] local folder_icon = [[]] -- .. directory if path ~= "" then local split = string.split(string.sub(path, 1, path:len() - 1), "/") table.remove(split, #split) if #split > 0 then -- deeper than 1 directory table.insert(files_table_data.rows, { string.format([[%s..]], folder_icon, repo.name, branch.name, table.concat(split, "/")), "","","" }) else -- only one directory deep table.insert(files_table_data.rows, { string.format([[%s..]], folder_icon, repo.name, branch.name), "","","" }) end end -- Regular directories for _, dir in pairs(files.dirs) do local lastedit = git.log(repo_dir, branch.name.." -1", dir, 1, 0, false)[1] local split = string.split(dir, "/") local name = split[#split] table.insert(files_table_data.rows, { string.format([[%s%s]], folder_icon, repo.name, branch.name, dir, name), utils.html_sanitize(lastedit.subject), utils.iso8601(lastedit.timestamp), string.format([[%s]], repo.name, lastedit.hash, lastedit.shorthash) }) end -- Regular files for _, file in pairs(files.files) do local lastedit = git.log(repo_dir, branch.name.." -1", file, 1, 0, false)[1] local split = string.split(file, "/") local name = split[#split] table.insert(files_table_data.rows, { string.format([[%s%s]], file_icon, repo.name, branch.name, file, name), utils.html_sanitize(lastedit.subject), utils.iso8601(lastedit.timestamp), string.format([[%s]], repo.name, lastedit.hash, lastedit.shorthash) }) end t.body = t.body..tabulate(files_table_data) -- Look for and render README if it exists for _, file in pairs(files.files) do local l = file:lower() if l:match("^readme") then t.body = t.body.."

README

" local text = git.show_file(repo_dir, branch.name, path..file) local s = file:len() if string.sub(l, s-2, s) == ".md" then t.body = t.body..[[
]]..utils.markdown(text).."
" else t.body = t.body.."
"..text.."
" end break end end return t end local refs = function(repo, repo_dir, branch) local t = create_html_template() -- Header with repository description and navigation links t.body = t.body..string.format([[

%s / refs

]], repo.name, repo.name, repo.name) t.body = t.body.."

"..repo.description.."

" local navlinks_list = { "Download", "Refs", "Commit Log", "Files" } for _, special in pairs(repo.specialfiles) do local split = string.split(special, " ") table.insert(navlinks_list, string.format([[%s]], repo.name, branch.name, split[2], split[1])) end t.body = t.body.."

"..table.concat(navlinks_list, " | ").."

" local all_refs = git.list_refs(repo_dir) -- Branches if #all_refs.heads > 0 then t.body = t.body.."

Branches

" local branches_table_data = {} branches_table_data.class = "branches" branches_table_data.headers = { {"name", "Name"}, {"ref", "Ref"}, {"has", "Hash"} } branches_table_data.rows = {} for _, b in pairs(all_refs.heads) do table.insert(branches_table_data.rows, { b.name ~= branch.name and b.name or b.name.." (HEAD)", string.format([[%s]], repo.name, b.name, b.full), string.format([[%s]], repo.name, b.hash, b.shorthash) }) end t.body = t.body..tabulate(branches_table_data) end -- Tags if #all_refs.tags > 0 then t.body = t.body.."

Tags

" local tags_table_data = {} tags_table_data.class = "tags" tags_table_data.headers = { {"name", "Name"}, {"ref", "Ref"}, {"has", "Hash"} } tags_table_data.rows = {} for _, t in pairs(all_refs.tags) do table.insert(tags_table_data.rows, { t.name ~= branch.name and t.name or t.name.." (HEAD)", string.format([[%s]], repo.name, t.name, t.full), string.format([[%s]], repo.name, t.hash, t.shorthash) }) end t.body = t.body..tabulate(tags_table_data) end return t end local log = function(repo, repo_dir, branch, n, skip) n = n or 40 skip = skip or 0 local t = create_html_template() -- Header with repository description and navigation links t.body = t.body..string.format([[

%s / %s / log

]], repo.name, repo.name, repo.name, branch.name, branch.name, repo.name, branch.name) t.body = t.body.."

"..repo.description.."

" local navlinks_list = { "Download", "Refs", "Commit Log", "Files" } for _, special in pairs(repo.specialfiles) do local split = string.split(special, " ") table.insert(navlinks_list, string.format([[%s]], repo.name, branch.name, split[2], split[1])) end t.body = t.body.."

"..table.concat(navlinks_list, " | ").."

" -- Latest Commit table t.body = t.body.."

Commits

" local commits_table_data = {} commits_table_data.class = "log" commits_table_data.headers = { {"timestamp", "Time"}, {"shorthash", "Hash"}, {"subject", "Subject"}, {"author", "Author"}, {"changed_files", [[#]]}, {"changed_plus", [[(+)]]}, {"changed_minus", [[(-)]]}, {"gpggood", [[GPG?]]} } commits_table_data.rows = {} local commits_head = git.log(repo_dir, branch.name, path, n, skip, true) for _, commit in pairs(commits_head) do table.insert(commits_table_data.rows, { utils.iso8601(commit.timestamp), string.format([[%s]], repo.name, commit.hash, commit.shorthash), utils.html_sanitize(commit.subject), string.format([[%s]], commit.email, utils.html_sanitize(commit.author)), commit.diff.num, commit.diff.plus, commit.diff.minus, commit.gpggood }) end t.body = t.body..tabulate(commits_table_data) return t end local download = function(repo, repo_dir, branch) local t = create_html_template() -- Header with repository description and navigation links t.body = t.body..string.format([[

%s / download

]], repo.name, repo.name, repo.name) t.body = t.body.."

"..repo.description.."

" local navlinks_list = { "Download", "Refs", "Commit Log", "Files" } for _, special in pairs(repo.specialfiles) do local split = string.split(special, " ") table.insert(navlinks_list, string.format([[%s]], repo.name, branch.name, split[2], split[1])) end t.body = t.body.."

"..table.concat(navlinks_list, " | ").."

" t.body = t.body.."

Download URLs

" local urls = {} urls.class = "download-urls" urls.headers = { {"protocol", "Protocol"}, {"url", "URL"} } urls.rows = {} for _, url in pairs(repo.download) do local split = string.split(url, " ") table.insert(urls.rows, {split[1], string.format([[%s]], split[2], split[2])}) end t.body = t.body..tabulate(urls) t.body = t.body.."

Websites

" local sites = {} sites.class = "websites" sites.headers = { {"name", "Website"}, {"url", "URL"} } sites.rows = {} for _, site in pairs(repo.urls) do local split = string.split(site, " ") table.insert(sites.rows, {split[1], string.format([[%s]], split[2], split[2])}) end t.body = t.body..tabulate(sites) return t end local _M = {} _M.tree = tree _M.refs = refs _M.log = log _M.download = download return _M