mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
Rename 'cannocial_name_form' to 'canonical_name_form'
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1433 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
+253
-246
@@ -59,12 +59,12 @@ String get_export_full_path(String& rel_name) {
|
||||
|
||||
// ----------------------------------------------------------------------------- : HTML
|
||||
|
||||
// An HTML tag
|
||||
struct Tag {
|
||||
Tag(const Char* open_tag, const Char* close_tag)
|
||||
: open_tag(open_tag), close_tag(close_tag), opened(0)
|
||||
{}
|
||||
const Char* open_tag; ///< The tags to insert in HTML "<tag>"
|
||||
// An HTML tag
|
||||
struct Tag {
|
||||
Tag(const Char* open_tag, const Char* close_tag)
|
||||
: open_tag(open_tag), close_tag(close_tag), opened(0)
|
||||
{}
|
||||
const Char* open_tag; ///< The tags to insert in HTML "<tag>"
|
||||
const Char* close_tag; ///< The tags to insert in HTML "</tag>"
|
||||
int opened; ///< How often is the tag opened in the input?
|
||||
/// Write an open or close tag to a string if needed
|
||||
@@ -80,198 +80,198 @@ struct Tag {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// A tag, or a close tag
|
||||
struct NegTag {
|
||||
Tag* tag;
|
||||
bool neg; // a close tag instead of an open tag
|
||||
NegTag(Tag* tag, bool neg) : tag(tag), neg(neg) {}
|
||||
};
|
||||
|
||||
DECLARE_TYPEOF_COLLECTION(NegTag);
|
||||
|
||||
/// A stack of opened HTML tags
|
||||
class TagStack {
|
||||
public:
|
||||
void open(String& ret, Tag& tag) {
|
||||
add(ret, NegTag(&tag, false));
|
||||
}
|
||||
void close(String& ret, Tag& tag) {
|
||||
add(ret, NegTag(&tag, true));
|
||||
}
|
||||
// Close all tags, should be called at end of input
|
||||
void close_all(String& ret) {
|
||||
// cancel out tags with pending tags
|
||||
write_pending_tags(ret);
|
||||
// close all open tags
|
||||
while (!tags.empty()) {
|
||||
tags.back()->write(ret, true);
|
||||
tags.pop_back();
|
||||
}
|
||||
}
|
||||
// Write all pending tags, should be called before non-tag output
|
||||
void write_pending_tags(String& ret) {
|
||||
FOR_EACH(t, pending_tags) {
|
||||
t.tag->write(ret, t.neg);
|
||||
if (!t.neg) tags.push_back(t.tag);
|
||||
}
|
||||
pending_tags.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
vector<Tag*> tags; ///< Tags opened in the html output
|
||||
vector<NegTag> pending_tags; ///< Tags opened in the tagged string, but not (yet) in the output
|
||||
|
||||
void add(String& ret, const NegTag& tag) {
|
||||
// Cancel out with pending tag?
|
||||
for (int i = (int)pending_tags.size() - 1 ; i >= 0 ; --i) {
|
||||
if (pending_tags[i].tag == tag.tag) {
|
||||
if (pending_tags[i].neg != tag.neg) {
|
||||
pending_tags.erase(pending_tags.begin() + i);
|
||||
return;
|
||||
} else {
|
||||
break; // look no further
|
||||
}
|
||||
}
|
||||
}
|
||||
// Cancel out with existing tag?
|
||||
if (tag.neg) {
|
||||
for (int i = (int)tags.size() - 1 ; i >= 0 ; --i) {
|
||||
if (tags[i] == tag.tag) {
|
||||
// cancel out with existing tag i, e.g. <b>:
|
||||
// situation was <a><b><c>text
|
||||
// situation will become <a><b><c>text</c></b><c>
|
||||
vector<NegTag> reopen;
|
||||
for (int j = (int)tags.size() - 1 ; j > i ; --j) {
|
||||
pending_tags.push_back(NegTag(tags[j], true)); // close tag, top down
|
||||
tags.pop_back();
|
||||
}
|
||||
pending_tags.push_back(tag); // now close tag i
|
||||
for (int j = i + 1 ; j < (int)tags.size() ; ++j) {
|
||||
pending_tags.push_back(NegTag(tags[j], false)); // reopen later, bottom up
|
||||
tags.pop_back();
|
||||
}
|
||||
tags.resize(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Just insert normally
|
||||
pending_tags.push_back(tag);
|
||||
}
|
||||
};
|
||||
|
||||
// html-escape a string
|
||||
String html_escape(const String& str) {
|
||||
String ret;
|
||||
FOR_EACH_CONST(c, str) {
|
||||
if (c == _('\1') || c == _('<')) { // escape <
|
||||
ret += _("<");
|
||||
} else if (c == _('>')) { // escape >
|
||||
ret += _(">");
|
||||
} else if (c == _('&')) { // escape &
|
||||
ret += _("&");
|
||||
} else if (c == _('\'')) { // escape '
|
||||
ret += _("'");
|
||||
} else if (c == _('\"')) { // escape "
|
||||
ret += _(""");
|
||||
} else if (c >= 0x80) { // escape non ascii
|
||||
ret += String(_("&#")) << (int)c << _(';');
|
||||
} else {
|
||||
ret += c;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
|
||||
// A tag, or a close tag
|
||||
struct NegTag {
|
||||
Tag* tag;
|
||||
bool neg; // a close tag instead of an open tag
|
||||
NegTag(Tag* tag, bool neg) : tag(tag), neg(neg) {}
|
||||
};
|
||||
|
||||
DECLARE_TYPEOF_COLLECTION(NegTag);
|
||||
|
||||
/// A stack of opened HTML tags
|
||||
class TagStack {
|
||||
public:
|
||||
void open(String& ret, Tag& tag) {
|
||||
add(ret, NegTag(&tag, false));
|
||||
}
|
||||
void close(String& ret, Tag& tag) {
|
||||
add(ret, NegTag(&tag, true));
|
||||
}
|
||||
// Close all tags, should be called at end of input
|
||||
void close_all(String& ret) {
|
||||
// cancel out tags with pending tags
|
||||
write_pending_tags(ret);
|
||||
// close all open tags
|
||||
while (!tags.empty()) {
|
||||
tags.back()->write(ret, true);
|
||||
tags.pop_back();
|
||||
}
|
||||
}
|
||||
// Write all pending tags, should be called before non-tag output
|
||||
void write_pending_tags(String& ret) {
|
||||
FOR_EACH(t, pending_tags) {
|
||||
t.tag->write(ret, t.neg);
|
||||
if (!t.neg) tags.push_back(t.tag);
|
||||
}
|
||||
pending_tags.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
vector<Tag*> tags; ///< Tags opened in the html output
|
||||
vector<NegTag> pending_tags; ///< Tags opened in the tagged string, but not (yet) in the output
|
||||
|
||||
void add(String& ret, const NegTag& tag) {
|
||||
// Cancel out with pending tag?
|
||||
for (int i = (int)pending_tags.size() - 1 ; i >= 0 ; --i) {
|
||||
if (pending_tags[i].tag == tag.tag) {
|
||||
if (pending_tags[i].neg != tag.neg) {
|
||||
pending_tags.erase(pending_tags.begin() + i);
|
||||
return;
|
||||
} else {
|
||||
break; // look no further
|
||||
}
|
||||
}
|
||||
}
|
||||
// Cancel out with existing tag?
|
||||
if (tag.neg) {
|
||||
for (int i = (int)tags.size() - 1 ; i >= 0 ; --i) {
|
||||
if (tags[i] == tag.tag) {
|
||||
// cancel out with existing tag i, e.g. <b>:
|
||||
// situation was <a><b><c>text
|
||||
// situation will become <a><b><c>text</c></b><c>
|
||||
vector<NegTag> reopen;
|
||||
for (int j = (int)tags.size() - 1 ; j > i ; --j) {
|
||||
pending_tags.push_back(NegTag(tags[j], true)); // close tag, top down
|
||||
tags.pop_back();
|
||||
}
|
||||
pending_tags.push_back(tag); // now close tag i
|
||||
for (int j = i + 1 ; j < (int)tags.size() ; ++j) {
|
||||
pending_tags.push_back(NegTag(tags[j], false)); // reopen later, bottom up
|
||||
tags.pop_back();
|
||||
}
|
||||
tags.resize(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Just insert normally
|
||||
pending_tags.push_back(tag);
|
||||
}
|
||||
};
|
||||
|
||||
// html-escape a string
|
||||
String html_escape(const String& str) {
|
||||
String ret;
|
||||
FOR_EACH_CONST(c, str) {
|
||||
if (c == _('\1') || c == _('<')) { // escape <
|
||||
ret += _("<");
|
||||
} else if (c == _('>')) { // escape >
|
||||
ret += _(">");
|
||||
} else if (c == _('&')) { // escape &
|
||||
ret += _("&");
|
||||
} else if (c == _('\'')) { // escape '
|
||||
ret += _("'");
|
||||
} else if (c == _('\"')) { // escape "
|
||||
ret += _(""");
|
||||
} else if (c >= 0x80) { // escape non ascii
|
||||
ret += String(_("&#")) << (int)c << _(';');
|
||||
} else {
|
||||
ret += c;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// write symbols to html
|
||||
String symbols_to_html(const String& str, SymbolFont& symbol_font, double size) {
|
||||
guard_export_info(_("symbols_to_html"));
|
||||
ExportInfo& ei = *export_info();
|
||||
vector<SymbolFont::DrawableSymbol> symbols;
|
||||
symbol_font.split(str, symbols);
|
||||
String html;
|
||||
FOR_EACH(sym, symbols) {
|
||||
String filename = symbol_font.name() + _("-") + clean_filename(sym.text) + _(".png");
|
||||
map<String,wxSize>::iterator it = ei.exported_images.find(filename);
|
||||
if (it == ei.exported_images.end()) {
|
||||
// save symbol image
|
||||
Image img = symbol_font.getImage(size, sym);
|
||||
|
||||
// write symbols to html
|
||||
String symbols_to_html(const String& str, SymbolFont& symbol_font, double size) {
|
||||
guard_export_info(_("symbols_to_html"));
|
||||
ExportInfo& ei = *export_info();
|
||||
vector<SymbolFont::DrawableSymbol> symbols;
|
||||
symbol_font.split(str, symbols);
|
||||
String html;
|
||||
FOR_EACH(sym, symbols) {
|
||||
String filename = symbol_font.name() + _("-") + clean_filename(sym.text) + _(".png");
|
||||
map<String,wxSize>::iterator it = ei.exported_images.find(filename);
|
||||
if (it == ei.exported_images.end()) {
|
||||
// save symbol image
|
||||
Image img = symbol_font.getImage(size, sym);
|
||||
wxFileName fn;
|
||||
fn.SetPath(ei.directory_absolute);
|
||||
fn.SetFullName(filename);
|
||||
img.SaveFile(fn.GetFullPath());
|
||||
it = ei.exported_images.insert(make_pair(filename, wxSize(img.GetWidth(), img.GetHeight()))).first;
|
||||
}
|
||||
html += _("<img src='") + filename + _("' alt='") + html_escape(sym.text)
|
||||
+ _("' width='") + (String() << it->second.x)
|
||||
+ _("' height='") + (String() << it->second.y) + _("'>");
|
||||
}
|
||||
return html;
|
||||
}
|
||||
|
||||
String to_html(const String& str_in, const SymbolFontP& symbol_font, double symbol_size) {
|
||||
String str = remove_tag_contents(str_in,_("<sep-soft"));
|
||||
String ret;
|
||||
Tag bold (_("<b>"), _("</b>")),
|
||||
italic(_("<i>"), _("</i>")),
|
||||
symbol(_("<span class=\"symbol\">"), _("</span>"));
|
||||
TagStack tags;
|
||||
String symbols;
|
||||
for (size_t i = 0 ; i < str.size() ; ) {
|
||||
Char c = str.GetChar(i);
|
||||
if (c == _('<')) {
|
||||
++i;
|
||||
if (is_substr(str, i, _("b"))) {
|
||||
tags.open (ret, bold);
|
||||
} else if (is_substr(str, i, _("/b"))) {
|
||||
tags.close(ret, bold);
|
||||
} else if (is_substr(str, i, _("i"))) {
|
||||
tags.open (ret, italic);
|
||||
} else if (is_substr(str, i, _("/i"))) {
|
||||
tags.close(ret, italic);
|
||||
} else if (is_substr(str, i, _("sym"))) {
|
||||
tags.open (ret, symbol);
|
||||
} else if (is_substr(str, i, _("/sym"))) {
|
||||
if (!symbols.empty()) {
|
||||
// write symbols in a special way
|
||||
tags.write_pending_tags(ret);
|
||||
ret += symbols_to_html(symbols, *symbol_font, symbol_size);
|
||||
symbols.clear();
|
||||
}
|
||||
tags.close(ret, symbol);
|
||||
}
|
||||
i = skip_tag(str, i-1);
|
||||
} else {
|
||||
// normal character
|
||||
tags.write_pending_tags(ret);
|
||||
++i;
|
||||
if (symbol.opened > 0 && symbol_font) {
|
||||
symbols += c; // write as symbols instead
|
||||
} else {
|
||||
c = untag_char(c);
|
||||
if (c == _('<')) { // escape <
|
||||
ret += _("<");
|
||||
} else if (c == _('&')) { // escape &
|
||||
ret += _("&");
|
||||
} else if (c >= 0x80) { // escape non ascii
|
||||
ret += String(_("&#")) << (int)c << _(';');
|
||||
} else if (c == _('\n')) {
|
||||
ret += _("<br>\n");
|
||||
} else {
|
||||
ret += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// end of input
|
||||
if (!symbols.empty()) {
|
||||
tags.write_pending_tags(ret);
|
||||
ret += symbols_to_html(symbols, *symbol_font, symbol_size);
|
||||
symbols.clear();
|
||||
}
|
||||
tags.close_all(ret);
|
||||
return ret;
|
||||
fn.SetFullName(filename);
|
||||
img.SaveFile(fn.GetFullPath());
|
||||
it = ei.exported_images.insert(make_pair(filename, wxSize(img.GetWidth(), img.GetHeight()))).first;
|
||||
}
|
||||
html += _("<img src='") + filename + _("' alt='") + html_escape(sym.text)
|
||||
+ _("' width='") + (String() << it->second.x)
|
||||
+ _("' height='") + (String() << it->second.y) + _("'>");
|
||||
}
|
||||
return html;
|
||||
}
|
||||
|
||||
String to_html(const String& str_in, const SymbolFontP& symbol_font, double symbol_size) {
|
||||
String str = remove_tag_contents(str_in,_("<sep-soft"));
|
||||
String ret;
|
||||
Tag bold (_("<b>"), _("</b>")),
|
||||
italic(_("<i>"), _("</i>")),
|
||||
symbol(_("<span class=\"symbol\">"), _("</span>"));
|
||||
TagStack tags;
|
||||
String symbols;
|
||||
for (size_t i = 0 ; i < str.size() ; ) {
|
||||
Char c = str.GetChar(i);
|
||||
if (c == _('<')) {
|
||||
++i;
|
||||
if (is_substr(str, i, _("b"))) {
|
||||
tags.open (ret, bold);
|
||||
} else if (is_substr(str, i, _("/b"))) {
|
||||
tags.close(ret, bold);
|
||||
} else if (is_substr(str, i, _("i"))) {
|
||||
tags.open (ret, italic);
|
||||
} else if (is_substr(str, i, _("/i"))) {
|
||||
tags.close(ret, italic);
|
||||
} else if (is_substr(str, i, _("sym"))) {
|
||||
tags.open (ret, symbol);
|
||||
} else if (is_substr(str, i, _("/sym"))) {
|
||||
if (!symbols.empty()) {
|
||||
// write symbols in a special way
|
||||
tags.write_pending_tags(ret);
|
||||
ret += symbols_to_html(symbols, *symbol_font, symbol_size);
|
||||
symbols.clear();
|
||||
}
|
||||
tags.close(ret, symbol);
|
||||
}
|
||||
i = skip_tag(str, i-1);
|
||||
} else {
|
||||
// normal character
|
||||
tags.write_pending_tags(ret);
|
||||
++i;
|
||||
if (symbol.opened > 0 && symbol_font) {
|
||||
symbols += c; // write as symbols instead
|
||||
} else {
|
||||
c = untag_char(c);
|
||||
if (c == _('<')) { // escape <
|
||||
ret += _("<");
|
||||
} else if (c == _('&')) { // escape &
|
||||
ret += _("&");
|
||||
} else if (c >= 0x80) { // escape non ascii
|
||||
ret += String(_("&#")) << (int)c << _(';');
|
||||
} else if (c == _('\n')) {
|
||||
ret += _("<br>\n");
|
||||
} else {
|
||||
ret += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// end of input
|
||||
if (!symbols.empty()) {
|
||||
tags.write_pending_tags(ret);
|
||||
ret += symbols_to_html(symbols, *symbol_font, symbol_size);
|
||||
symbols.clear();
|
||||
}
|
||||
tags.close_all(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// convert a tagged string to html
|
||||
@@ -300,57 +300,57 @@ SCRIPT_FUNCTION(symbols_to_html) {
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : BB Code
|
||||
|
||||
String to_bbcode(const String& str_in) {
|
||||
String str = remove_tag_contents(str_in,_("<sep-soft"));
|
||||
String ret;
|
||||
Tag bold (_("[b]"), _("[/b]")),
|
||||
italic(_("[i]"), _("[/i]"));
|
||||
TagStack tags;
|
||||
String symbols;
|
||||
for (size_t i = 0 ; i < str.size() ; ) {
|
||||
Char c = str.GetChar(i);
|
||||
if (c == _('<')) {
|
||||
++i;
|
||||
if (is_substr(str, i, _("b"))) {
|
||||
tags.open (ret, bold);
|
||||
} else if (is_substr(str, i, _("/b"))) {
|
||||
tags.close(ret, bold);
|
||||
} else if (is_substr(str, i, _("i"))) {
|
||||
tags.open (ret, italic);
|
||||
} else if (is_substr(str, i, _("/i"))) {
|
||||
tags.close(ret, italic);
|
||||
} /*else if (is_substr(str, i, _("sym"))) {
|
||||
tags.open (ret, symbol);
|
||||
} else if (is_substr(str, i, _("/sym"))) {
|
||||
if (!symbols.empty()) {
|
||||
// write symbols in a special way
|
||||
tags.write_pending_tags(ret);
|
||||
ret += symbols_to_html(symbols, symbol_font);
|
||||
symbols.clear();
|
||||
}
|
||||
tags.close(ret, symbol);
|
||||
}*/
|
||||
i = skip_tag(str, i-1);
|
||||
} else {
|
||||
// normal character
|
||||
tags.write_pending_tags(ret);
|
||||
++i;
|
||||
// if (symbol.opened > 0 && symbol_font) {
|
||||
// symbols += c; // write as symbols instead
|
||||
// } else {
|
||||
ret += untag_char(c);
|
||||
// }
|
||||
}
|
||||
}
|
||||
// end of input
|
||||
/* if (!symbols.empty()) {
|
||||
tags.write_pending_tags(ret);
|
||||
ret += symbols_to_html(symbols, symbol_font);
|
||||
symbols.clear();
|
||||
}*/
|
||||
tags.close_all(ret);
|
||||
return ret;
|
||||
|
||||
String to_bbcode(const String& str_in) {
|
||||
String str = remove_tag_contents(str_in,_("<sep-soft"));
|
||||
String ret;
|
||||
Tag bold (_("[b]"), _("[/b]")),
|
||||
italic(_("[i]"), _("[/i]"));
|
||||
TagStack tags;
|
||||
String symbols;
|
||||
for (size_t i = 0 ; i < str.size() ; ) {
|
||||
Char c = str.GetChar(i);
|
||||
if (c == _('<')) {
|
||||
++i;
|
||||
if (is_substr(str, i, _("b"))) {
|
||||
tags.open (ret, bold);
|
||||
} else if (is_substr(str, i, _("/b"))) {
|
||||
tags.close(ret, bold);
|
||||
} else if (is_substr(str, i, _("i"))) {
|
||||
tags.open (ret, italic);
|
||||
} else if (is_substr(str, i, _("/i"))) {
|
||||
tags.close(ret, italic);
|
||||
} /*else if (is_substr(str, i, _("sym"))) {
|
||||
tags.open (ret, symbol);
|
||||
} else if (is_substr(str, i, _("/sym"))) {
|
||||
if (!symbols.empty()) {
|
||||
// write symbols in a special way
|
||||
tags.write_pending_tags(ret);
|
||||
ret += symbols_to_html(symbols, symbol_font);
|
||||
symbols.clear();
|
||||
}
|
||||
tags.close(ret, symbol);
|
||||
}*/
|
||||
i = skip_tag(str, i-1);
|
||||
} else {
|
||||
// normal character
|
||||
tags.write_pending_tags(ret);
|
||||
++i;
|
||||
// if (symbol.opened > 0 && symbol_font) {
|
||||
// symbols += c; // write as symbols instead
|
||||
// } else {
|
||||
ret += untag_char(c);
|
||||
// }
|
||||
}
|
||||
}
|
||||
// end of input
|
||||
/* if (!symbols.empty()) {
|
||||
tags.write_pending_tags(ret);
|
||||
ret += symbols_to_html(symbols, symbol_font);
|
||||
symbols.clear();
|
||||
}*/
|
||||
tags.close_all(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// convert a tagged string to BBCode
|
||||
@@ -443,6 +443,12 @@ SCRIPT_FUNCTION(write_set_file) {
|
||||
|
||||
}
|
||||
|
||||
SCRIPT_FUNCTION(sanitize) {
|
||||
SCRIPT_PARAM_C(String, input);
|
||||
//TODO
|
||||
SCRIPT_RETURN(input);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Init
|
||||
|
||||
void init_script_export_functions(Context& ctx) {
|
||||
@@ -453,4 +459,5 @@ void init_script_export_functions(Context& ctx) {
|
||||
ctx.setVariable(_("write text file"), script_write_text_file);
|
||||
ctx.setVariable(_("write image file"), script_write_image_file);
|
||||
ctx.setVariable(_("write set file"), script_write_set_file);
|
||||
ctx.setVariable(_("sanitize"), script_sanitize);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user