mirror of
https://github.com/bsnes-emu/bsnes.git
synced 2025-08-21 06:41:40 +02:00
Update to v094r37 release.
byuu says: Changelog: - synchronizes lots of nall changes - changes displayed program title from tomoko to higan(*) - browser dialog sort is case-insensitive - .sys folders look at user-selected library path; no longer hard-coded Tried to get rid of the file modes from the Windows browser dialog, but it was being a bitch so I left it on for now. - The storage locations and binary still use tomoko. I'm not really sure what to do here. The idea is there may be more than one "higan" UI in the future, but I don't want people to go around calling the entire program by the UI name. For official Windows releases, I can rename the binaries to "higan-{profile}.exe", and by putting the config files with the binary, they won't ever see the tomoko folder. Linux is of course trickier. Note: Windows users will need to edit hiro/components.hpp and comment out these lines: #define Hiro_Console #define Hiro_IconView #define Hiro_SourceView #define Hiro_TreeView I forgot to do that, and too lazy to upload another WIP.
This commit is contained in:
@@ -1,92 +1,104 @@
|
||||
#ifdef NALL_STRING_INTERNAL_HPP
|
||||
|
||||
/* Document Markup Language (DML) v1.0 parser
|
||||
* revision 0.01
|
||||
* revision 0.03
|
||||
*/
|
||||
|
||||
namespace nall { namespace {
|
||||
|
||||
struct DML {
|
||||
auto& setAllowHTML(bool allowHTML) { settings.allowHTML = allowHTML; return *this; }
|
||||
auto& setHost(const string& hostname) { settings.host = {hostname, "/"}; return *this; }
|
||||
auto& setPath(const string& pathname) { settings.path = pathname; return *this; }
|
||||
auto& setReader(const function<string (string)>& reader) { settings.reader = reader; return *this; }
|
||||
auto& setSectioned(bool sectioned) { settings.sectioned = sectioned; return *this; }
|
||||
|
||||
auto parse(const string& filedata, const string& pathname) -> string;
|
||||
auto parse(const string& filename) -> string;
|
||||
|
||||
private:
|
||||
struct Settings {
|
||||
bool allowHTML = true;
|
||||
string host = "localhost/";
|
||||
string path;
|
||||
function<string (string)> reader;
|
||||
bool sectioned = true;
|
||||
} settings;
|
||||
|
||||
DML(const string& filedata, const string& pathname);
|
||||
DML(const string& filename);
|
||||
auto output() -> string;
|
||||
|
||||
private:
|
||||
struct State {
|
||||
string output;
|
||||
unsigned sections = 0;
|
||||
} state;
|
||||
|
||||
auto parse(const string& filedata, const string& pathname) -> bool;
|
||||
auto parseBlock(string& block, const string& pathname) -> bool;
|
||||
auto parseDocument(const string& filedata, const string& pathname, unsigned depth) -> bool;
|
||||
auto parseBlock(string& block, const string& pathname, unsigned depth) -> bool;
|
||||
auto count(const string& text, char value) -> unsigned;
|
||||
|
||||
auto escape(const string& text) -> string;
|
||||
auto markup(const string& text) -> string;
|
||||
};
|
||||
|
||||
DML::DML(const string& filedata, const string& pathname) {
|
||||
parse(filedata, pathname);
|
||||
}
|
||||
|
||||
DML::DML(const string& filename) {
|
||||
parse(string::read(filename), filename.pathname());
|
||||
}
|
||||
|
||||
auto DML::output() -> string {
|
||||
auto DML::parse(const string& filedata, const string& pathname) -> string {
|
||||
settings.path = pathname;
|
||||
parseDocument(filedata, settings.path, 0);
|
||||
return state.output;
|
||||
}
|
||||
|
||||
auto DML::parse(const string& filedata, const string& pathname) -> bool {
|
||||
auto DML::parse(const string& filename) -> string {
|
||||
if(!settings.path) settings.path = filename.pathname();
|
||||
string document = settings.reader ? settings.reader(filename) : string::read(filename);
|
||||
parseDocument(document, settings.path, 0);
|
||||
return state.output;
|
||||
}
|
||||
|
||||
auto DML::parseDocument(const string& filedata, const string& pathname, unsigned depth) -> bool {
|
||||
if(depth >= 100) return false; //attempt to prevent infinite recursion with reasonable limit
|
||||
|
||||
auto blocks = filedata.split("\n\n");
|
||||
for(auto& block : blocks) parseBlock(block, pathname);
|
||||
if(settings.sectioned && state.sections) state.output.append("</section>\n");
|
||||
for(auto& block : blocks) parseBlock(block, pathname, depth);
|
||||
if(settings.sectioned && state.sections && depth == 0) state.output.append("</section>\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
auto DML::parseBlock(string& block, const string& pathname) -> bool {
|
||||
auto DML::parseBlock(string& block, const string& pathname, unsigned depth) -> bool {
|
||||
if(block.rstrip().empty()) return true;
|
||||
auto lines = block.split("\n");
|
||||
|
||||
//include
|
||||
if(block.beginsWith("{{include}}")) {
|
||||
string filename{pathname, block.ltrim("{{include}}").strip()};
|
||||
parse(string::read(filename), filename.pathname());
|
||||
if(block.beginsWith("<include ") && block.endsWith(">")) {
|
||||
string filename{pathname, block.trim("<include ", ">", 1L).strip()};
|
||||
string document = settings.reader ? settings.reader(filename) : string::read(filename);
|
||||
parseDocument(document, filename.pathname(), depth + 1);
|
||||
}
|
||||
|
||||
//html
|
||||
else if(ltrim(block, "{{html}}") && settings.allowHTML) {
|
||||
auto data = lines.takeFirst();
|
||||
if(ltrim(data, "{{html}} ")) state.output.append(data, "\n");
|
||||
for(auto& line : lines) {
|
||||
if(ltrim(line, " ")) state.output.append(line, "\n");
|
||||
else if(block.beginsWith("<html>\n") && settings.allowHTML) {
|
||||
for(auto n : range(lines)) {
|
||||
if(n == 0 || !lines[n].beginsWith(" ")) continue;
|
||||
state.output.append(lines[n].ltrim(" ", 1L), "\n");
|
||||
}
|
||||
}
|
||||
|
||||
//header
|
||||
//section
|
||||
else if(block.beginsWith("# ")) {
|
||||
if(settings.sectioned) {
|
||||
if(state.sections++) state.output.append("</section>");
|
||||
state.output.append("<section>");
|
||||
}
|
||||
auto content = lines.takeFirst().ltrim("# ").split<1>(" => ");
|
||||
auto content = lines.takeFirst().ltrim("# ", 1L).split(" => ", 1L);
|
||||
auto data = markup(content[0]);
|
||||
auto name = escape(content(1, data.crc32()));
|
||||
state.output.append("<header id=\"", name, "\">", data);
|
||||
for(auto& line : lines) {
|
||||
if(!line.beginsWith("# ")) continue;
|
||||
state.output.append("<span>", line.ltrim("# "), "</span>");
|
||||
state.output.append("<span>", line.ltrim("# ", 1L), "</span>");
|
||||
}
|
||||
state.output.append("</header>\n");
|
||||
}
|
||||
|
||||
//subheader
|
||||
//header
|
||||
else if(auto depth = count(block, '=')) {
|
||||
auto content = lines.takeFirst().slice(depth + 1).split<1>(" => ");
|
||||
auto content = lines.takeFirst().slice(depth + 1).split(" => ", 1L);
|
||||
auto data = markup(content[0]);
|
||||
auto name = escape(content(1, data.crc32()));
|
||||
if(depth <= 6) {
|
||||
@@ -99,7 +111,7 @@ auto DML::parseBlock(string& block, const string& pathname) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
//contents
|
||||
//navigation
|
||||
else if(count(block, '-')) {
|
||||
state.output.append("<nav>\n");
|
||||
unsigned level = 0;
|
||||
@@ -107,7 +119,7 @@ auto DML::parseBlock(string& block, const string& pathname) -> bool {
|
||||
if(auto depth = count(line, '-')) {
|
||||
while(level < depth) level++, state.output.append("<ul>\n");
|
||||
while(level > depth) level--, state.output.append("</ul>\n");
|
||||
auto content = line.slice(depth + 1).split<1>(" => ");
|
||||
auto content = line.slice(depth + 1).split(" => ", 1L);
|
||||
auto data = markup(content[0]);
|
||||
auto name = escape(content(1, data.crc32()));
|
||||
state.output.append("<li><a href=\"#", name, "\">", data, "</a></li>\n");
|
||||
@@ -149,10 +161,10 @@ auto DML::parseBlock(string& block, const string& pathname) -> bool {
|
||||
else if(block.beginsWith(" ")) {
|
||||
state.output.append("<pre>");
|
||||
for(auto& line : lines) {
|
||||
if(!ltrim(line, " ")) continue;
|
||||
state.output.append(escape(line), "\n");
|
||||
if(!line.beginsWith(" ")) continue;
|
||||
state.output.append(escape(line.ltrim(" ", 1L)), "\n");
|
||||
}
|
||||
state.output.rtrim("\n").append("</pre>\n");
|
||||
state.output.rtrim("\n", 1L).append("</pre>\n");
|
||||
}
|
||||
|
||||
//divider
|
||||
@@ -180,77 +192,75 @@ auto DML::count(const string& text, char value) -> unsigned {
|
||||
|
||||
auto DML::escape(const string& text) -> string {
|
||||
string output;
|
||||
for(unsigned n = 0; n < text.size();) {
|
||||
char x = text[n++];
|
||||
if(x == '&') { output.append("&"); continue; }
|
||||
if(x == '<') { output.append("<"); continue; }
|
||||
if(x == '>') { output.append(">"); continue; }
|
||||
if(x == '"') { output.append("""); continue; }
|
||||
output.append(x);
|
||||
for(auto c : text) {
|
||||
if(c == '&') { output.append("&"); continue; }
|
||||
if(c == '<') { output.append("<"); continue; }
|
||||
if(c == '>') { output.append(">"); continue; }
|
||||
if(c == '"') { output.append("""); continue; }
|
||||
output.append(c);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
auto DML::markup(const string& text) -> string {
|
||||
string output;
|
||||
char flagStrong = 0;
|
||||
char flagEmphasis = 0;
|
||||
char flagInsert = 0;
|
||||
char flagDelete = 0;
|
||||
char flagCode = 0;
|
||||
|
||||
char match = 0;
|
||||
unsigned offset = 0;
|
||||
for(unsigned n = 0; n < text.size();) {
|
||||
char x = text[n], y = text[n + 1];
|
||||
char a = n ? text[n - 1] : 0;
|
||||
char b = text[n];
|
||||
char c = text[n++ + 1];
|
||||
|
||||
if(x == '[' && y == '\\') { output.append('['); n += 2; continue; }
|
||||
bool d = !a || a == ' ' || a == '\t' || a == '\r' || a == '\n'; //is previous character whitespace?
|
||||
bool e = !c || c == ' ' || c == '\t' || c == '\r' || c == '\n'; //is next character whitespace?
|
||||
bool f = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'); //is next character alphanumeric?
|
||||
|
||||
if(x == '[' && y == '*' && flagStrong == 0) { flagStrong = 1; output.append("<strong>"); n += 2; continue; }
|
||||
if(x == '*' && y == ']' && flagStrong == 1) { flagStrong = 0; output.append("</strong>"); n += 2; continue; }
|
||||
|
||||
if(x == '[' && y == '/' && flagEmphasis == 0) { flagEmphasis = 1; output.append("<em>"); n += 2; continue; }
|
||||
if(x == '/' && y == ']' && flagEmphasis == 1) { flagEmphasis = 0; output.append("</em>"); n += 2; continue; }
|
||||
|
||||
if(x == '[' && y == '_' && flagInsert == 0) { flagInsert = 1; output.append("<ins>"); n += 2; continue; }
|
||||
if(x == '_' && y == ']' && flagInsert == 1) { flagInsert = 0; output.append("</ins>"); n += 2; continue; }
|
||||
|
||||
if(x == '[' && y == '-' && flagDelete == 0) { flagDelete = 1; output.append("<del>"); n += 2; continue; }
|
||||
if(x == '-' && y == ']' && flagDelete == 1) { flagDelete = 0; output.append("</del>"); n += 2; continue; }
|
||||
|
||||
if(x == '[' && y == '|' && flagCode == 0) { flagCode = 1; output.append("<code>"); n += 2; continue; }
|
||||
if(x == '|' && y == ']' && flagCode == 1) { flagCode = 0; output.append("</code>"); n += 2; continue; }
|
||||
|
||||
if(x == '[' && y == '[') {
|
||||
if(auto length = text.findFrom(n + 2, "]]")) {
|
||||
lstring content = text.slice(n + 2, *length).split<1>(" => ");
|
||||
output.append("<a href=\"", escape(content[0]), "\">", escape(content(1, content[0])), "</a>");
|
||||
n += *length + 4;
|
||||
continue;
|
||||
}
|
||||
if(!match && d && !e) {
|
||||
if(b == '*') { match = '*'; offset = n; continue; }
|
||||
if(b == '/') { match = '/'; offset = n; continue; }
|
||||
if(b == '_') { match = '_'; offset = n; continue; }
|
||||
if(b == '~') { match = '~'; offset = n; continue; }
|
||||
if(b == '|') { match = '|'; offset = n; continue; }
|
||||
if(b == '[') { match = ']'; offset = n; continue; }
|
||||
if(b == '{') { match = '}'; offset = n; continue; }
|
||||
}
|
||||
|
||||
if(x == '[' && y == '{') {
|
||||
if(auto length = text.findFrom(n + 2, "}]")) {
|
||||
lstring content = text.slice(n + 2, *length).split<1>(" => ");
|
||||
output.append("<img src=\"", escape(content[0]), "\" alt=\"", escape(content(1, "")), "\">");
|
||||
n += *length + 4;
|
||||
//if we reach the end of the string without a match; force a match so the content is still output
|
||||
if(match && b != match && !c) { b = match; f = 0; n++; }
|
||||
|
||||
if(match && b == match && !f) {
|
||||
match = 0;
|
||||
auto content = text.slice(offset, n - offset - 1);
|
||||
if(b == '*') { output.append("<strong>", escape(content), "</strong>"); continue; }
|
||||
if(b == '/') { output.append("<em>", escape(content), "</em>"); continue; }
|
||||
if(b == '_') { output.append("<ins>", escape(content), "</ins>"); continue; }
|
||||
if(b == '~') { output.append("<del>", escape(content), "</del>"); continue; }
|
||||
if(b == '|') { output.append("<code>", escape(content), "</code>"); continue; }
|
||||
if(b == ']') {
|
||||
auto p = content.split(" => ", 1L);
|
||||
p[0].replace("@/", settings.host);
|
||||
output.append("<a href=\"", escape(p[0]), "\">", escape(p(1, p[0])), "</a>");
|
||||
continue;
|
||||
}
|
||||
if(b == '}') {
|
||||
auto p = content.split(" => ", 1L);
|
||||
p[0].replace("@/", settings.host);
|
||||
output.append("<img src=\"", escape(p[0]), "\" alt=\"", escape(p(1, "")), "\">");
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if(x == '&') { output.append("&"); n++; continue; }
|
||||
if(x == '<') { output.append("<"); n++; continue; }
|
||||
if(x == '>') { output.append(">"); n++; continue; }
|
||||
if(x == '"') { output.append("""); n++; continue; }
|
||||
|
||||
output.append(x);
|
||||
n++;
|
||||
if(match) continue;
|
||||
if(b == '\\' && c) { output.append(c); n++; continue; } //character escaping
|
||||
if(b == '&') { output.append("&"); continue; } //entity escaping
|
||||
if(b == '<') { output.append("<"); continue; } //...
|
||||
if(b == '>') { output.append(">"); continue; } //...
|
||||
if(b == '"') { output.append("""); continue; } //...
|
||||
output.append(b);
|
||||
}
|
||||
|
||||
if(flagStrong) output.append("</strong>");
|
||||
if(flagEmphasis) output.append("</em>");
|
||||
if(flagInsert) output.append("</ins>");
|
||||
if(flagDelete) output.append("</del>");
|
||||
if(flagCode) output.append("</code>");
|
||||
return output;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user