uce/site/doc/index.uce

338 lines
7.4 KiB
Plaintext

struct DocPage {
String title;
String content;
StringList sig_lines;
StringList param_lines;
StringList see_lines;
};
String doc_default_title(String page)
{
String page_title = page;
if(page_title.length() > 1 && page_title[1] == '_')
nibble(page_title, "_");
return(page_title);
}
String doc_markdown_inline(String text)
{
text = trim(text);
if(text == "")
return("");
String html = markdown_to_html(text);
if(html.length() >= 7 && html.substr(0, 3) == "<p>" && html.substr(html.length() - 4) == "</p>")
return(html.substr(3, html.length() - 7));
return(html);
}
String doc_legacy_heading(String section)
{
if(section == "desc")
return("");
if(section == "related")
return("## PHP & JS Equivalents");
return("## " + section);
}
DocPage load_doc_page(String page)
{
DocPage result;
StringList lines = split(file_get_contents("pages/" + page + ".txt"), "\n");
String current_section = "";
bool content_mode = false;
StringList content_lines;
for(auto line : lines)
{
if(!content_mode && line != "" && line.substr(0, 1) == ":")
{
String section = trim(line.substr(1));
if(section == "title" || section == "sig" || section == "params" || section == "see")
{
current_section = section;
continue;
}
if(section == "content")
{
content_mode = true;
current_section = "content";
continue;
}
current_section = "legacy";
String heading = doc_legacy_heading(section);
if(heading != "")
{
if(content_lines.size() > 0 && content_lines.back() != "")
content_lines.push_back("");
content_lines.push_back(heading);
content_lines.push_back("");
}
continue;
}
if(current_section == "title")
{
if(result.title != "")
result.title += "\n";
result.title += line;
}
else if(current_section == "sig")
{
result.sig_lines.push_back(line);
}
else if(current_section == "params")
{
result.param_lines.push_back(line);
}
else if(current_section == "see")
{
if(trim(line) != "")
result.see_lines.push_back(trim(line));
}
else
{
content_lines.push_back(line);
}
}
result.content = join(content_lines, "\n");
result.title = trim(result.title);
return(result);
}
void render_doc_params(StringList param_lines)
{
if(param_lines.size() == 0)
return;
<><div class="doc-section params">
<h3>Parameters</h3><?
for(auto line : param_lines)
{
if(trim(line) == "")
continue;
String name = trim(nibble(line, ":"));
String value = trim(line);
?><div><b><?= name ?></b><?
if(value != "")
{
?> : <?: doc_markdown_inline(value) ?><?
}
?></div><?
}
?></div></>
}
void render_see_section(String name)
{
StringList lines = split(file_get_contents("areas/" + name + ".txt"), "\n");
s32 idx = 0;
for(auto line : lines)
{
if(idx == 0)
{
<><div class="category"><h3><?= line ?></h3><ul></>
}
else if(line != "")
{
<><li><a href="index.uce?p=<?= uri_encode(line) ?>"><?= line ?><span class="dim">()</span></a></li></>
}
idx += 1;
}
<></ul></div></>
}
void render_doc_see_links(StringList see_lines)
{
if(see_lines.size() == 0)
return;
<><h3 class="sidebar-subhead">Related</h3>
<?
for(auto sl : see_lines)
{
if(sl[0] == '>')
{
render_see_section(sl.substr(1));
}
else
{
?><div><a href="index.uce?p=<?= trim(sl) ?>"><?= trim(sl) ?><span class="dim">()</span></a></div><?
}
}
?>
</>
}
RENDER(Request& context)
{
String page = first(context.get["p"], "index");
DocPage doc_page;
String page_title = doc_default_title(page);
if(page != "index")
{
doc_page = load_doc_page(page);
if(doc_page.title != "")
page_title = doc_page.title;
}
<><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"></meta>
<link rel="stylesheet" href='style.css?v=<?= time() ?>'></link>
</head>
<body>
<h1>
<a href="index.uce">UCE Docs</a><?
if(page != "index")
{
?><span class="dim"> / </span><?= page_title ?><?
}
?>
</h1>
<?
if(page == "index")
{
?><div class="search-bar">
<input id="doc-search" type="search" placeholder="Search API…" autocomplete="off" spellcheck="false"/>
<span class="search-count" id="search-count"></span>
</div>
<div id="search-results" hidden></div>
<div class="index-pane" id="index-pane">
<div class="index-layout">
<nav class="sidebar"><?
for(auto file_name : ls("areas/"))
{
String ft = nibble(file_name, ".");
render_see_section(ft);
}
?></nav>
<main class="content">
<h2>All API Functions</h2>
<div class="func-grid"><?
for(auto file_name : ls("pages/"))
{
String ft = nibble(file_name, ".");
if(ft.substr(0, 2) == "0_")
{
String fn = ft;
String pre = nibble(fn, "_");
?>
<div class="func-item"><a href="?p=<?= uri_encode(ft) ?>"><?= fn ?><span class="badge">struct</span></a></div>
<?
}
else if(ft.substr(0, 2) == "1_")
{
String fn = ft;
String pre = nibble(fn, "_");
?>
<div class="func-item"><a href="?p=<?= uri_encode(ft) ?>"><?= fn ?><span class="badge">directive</span></a></div>
<?
}
else if(ft.substr(0, 2) == "3_")
{
String fn = ft;
String pre = nibble(fn, "_");
?>
<div class="func-item"><a href="?p=<?= uri_encode(ft) ?>"><?= fn ?><span class="badge">info</span></a></div>
<?
}
else
{
?>
<div class="func-item"><a href="?p=<?= uri_encode(ft) ?>"><?= ft ?><span class="dim">()</span></a></div>
<?
}
}
?></div>
</main>
</div>
</div>
<?
<><script>
(function () {
var input = document.getElementById('doc-search');
var results = document.getElementById('search-results');
var pane = document.getElementById('index-pane');
var counter = document.getElementById('search-count');
var timer;
function showResults(html) {
results.innerHTML = html;
results.hidden = false;
pane.hidden = true;
var hits = results.querySelectorAll('.search-hit').length;
counter.textContent = hits ? hits + ' result' + (hits > 1 ? 's' : '') : '';
}
function showIndex() {
results.innerHTML = '';
results.hidden = true;
pane.hidden = false;
counter.textContent = '';
}
input.addEventListener('input', function () {
var q = this.value.trim();
clearTimeout(timer);
if (q.length < 2) { showIndex(); return; }
counter.textContent = '\u2026';
timer = setTimeout(function () {
fetch('search.uce?q=' + encodeURIComponent(q))
.then(function (r) { return r.text(); })
.then(showResults);
}, 220);
});
input.addEventListener('keydown', function (e) {
if (e.key === 'Escape') { this.value = ''; showIndex(); }
});
})();
</script></>
}
else
{
String detail_class = "detail-layout";
if(doc_page.see_lines.size() == 0)
detail_class += " no-sidebar";
?><div class="<?= detail_class ?>">
<article class="doc-detail"><?
if(doc_page.sig_lines.size() > 0)
{
?><div class="doc-section sig">
<h3>Signature</h3>
<pre><? print(join(doc_page.sig_lines, "\n")); ?></pre>
</div><?
}
render_doc_params(doc_page.param_lines);
if(trim(doc_page.content) != "")
{
?><div class="doc-section content"><?: markdown_to_html(doc_page.content) ?></div><?
}
?></article><?
if(doc_page.see_lines.size() > 0)
{
?><aside class="detail-sidebar">
<div class="sidebar-card"><?
render_doc_see_links(doc_page.see_lines);
?></div>
</aside><?
}
?></div><?
}
?>
</body>
</html></>
}