Moved doc description formatting. Fixes #12798

Paragraph spacing is now applied only during rendering (so doctool
preserves formatting).

Paragraph spacing now no longer applies within [code] tags.

Extra bbcode is now ignored within [code] tags.
This commit is contained in:
Zher Huei Lee 2017-11-11 09:11:24 +08:00
parent 19b1ff0fc5
commit 34f4ae18b8
2 changed files with 36 additions and 35 deletions

View File

@ -615,11 +615,6 @@ void DocData::generate(bool p_basic_types) {
} }
} }
static String _format_description(const String &string) {
return string.dedent().strip_edges().replace("\n", "\n\n");
}
static Error _parse_methods(Ref<XMLParser> &parser, Vector<DocData::MethodDoc> &methods) { static Error _parse_methods(Ref<XMLParser> &parser, Vector<DocData::MethodDoc> &methods) {
String section = parser->get_node_name(); String section = parser->get_node_name();
@ -666,7 +661,7 @@ static Error _parse_methods(Ref<XMLParser> &parser, Vector<DocData::MethodDoc> &
parser->read(); parser->read();
if (parser->get_node_type() == XMLParser::NODE_TEXT) if (parser->get_node_type() == XMLParser::NODE_TEXT)
method.description = _format_description(parser->get_node_data()); method.description = parser->get_node_data();
} }
} else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == element) } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == element)
@ -781,20 +776,20 @@ Error DocData::_load(Ref<XMLParser> parser) {
parser->read(); parser->read();
if (parser->get_node_type() == XMLParser::NODE_TEXT) if (parser->get_node_type() == XMLParser::NODE_TEXT)
c.brief_description = _format_description(parser->get_node_data()); c.brief_description = parser->get_node_data();
} else if (name == "description") { } else if (name == "description") {
parser->read(); parser->read();
if (parser->get_node_type() == XMLParser::NODE_TEXT) if (parser->get_node_type() == XMLParser::NODE_TEXT)
c.description = _format_description(parser->get_node_data()); c.description = parser->get_node_data();
} else if (name == "tutorials") { } else if (name == "tutorials") {
parser->read(); parser->read();
if (parser->get_node_type() == XMLParser::NODE_TEXT) if (parser->get_node_type() == XMLParser::NODE_TEXT)
c.tutorials = parser->get_node_data().strip_edges(); c.tutorials = parser->get_node_data();
} else if (name == "demos") { } else if (name == "demos") {
parser->read(); parser->read();
if (parser->get_node_type() == XMLParser::NODE_TEXT) if (parser->get_node_type() == XMLParser::NODE_TEXT)
c.demos = parser->get_node_data().strip_edges(); c.demos = parser->get_node_data();
} else if (name == "methods") { } else if (name == "methods") {
Error err = _parse_methods(parser, c.methods); Error err = _parse_methods(parser, c.methods);
@ -828,7 +823,7 @@ Error DocData::_load(Ref<XMLParser> parser) {
prop.enumeration = parser->get_attribute_value("enum"); prop.enumeration = parser->get_attribute_value("enum");
parser->read(); parser->read();
if (parser->get_node_type() == XMLParser::NODE_TEXT) if (parser->get_node_type() == XMLParser::NODE_TEXT)
prop.description = _format_description(parser->get_node_data()); prop.description = parser->get_node_data();
c.properties.push_back(prop); c.properties.push_back(prop);
} else { } else {
ERR_EXPLAIN("Invalid tag in doc file: " + name); ERR_EXPLAIN("Invalid tag in doc file: " + name);
@ -857,7 +852,7 @@ Error DocData::_load(Ref<XMLParser> parser) {
prop.type = parser->get_attribute_value("type"); prop.type = parser->get_attribute_value("type");
parser->read(); parser->read();
if (parser->get_node_type() == XMLParser::NODE_TEXT) if (parser->get_node_type() == XMLParser::NODE_TEXT)
prop.description = parser->get_node_data().strip_edges(); prop.description = parser->get_node_data();
c.theme_properties.push_back(prop); c.theme_properties.push_back(prop);
} else { } else {
ERR_EXPLAIN("Invalid tag in doc file: " + name); ERR_EXPLAIN("Invalid tag in doc file: " + name);
@ -888,7 +883,7 @@ Error DocData::_load(Ref<XMLParser> parser) {
} }
parser->read(); parser->read();
if (parser->get_node_type() == XMLParser::NODE_TEXT) if (parser->get_node_type() == XMLParser::NODE_TEXT)
constant.description = parser->get_node_data().strip_edges(); constant.description = parser->get_node_data();
c.constants.push_back(constant); c.constants.push_back(constant);
} else { } else {
ERR_EXPLAIN("Invalid tag in doc file: " + name); ERR_EXPLAIN("Invalid tag in doc file: " + name);
@ -915,6 +910,8 @@ Error DocData::_load(Ref<XMLParser> parser) {
static void _write_string(FileAccess *f, int p_tablevel, const String &p_string) { static void _write_string(FileAccess *f, int p_tablevel, const String &p_string) {
if (p_string == "")
return;
String tab; String tab;
for (int i = 0; i < p_tablevel; i++) for (int i = 0; i < p_tablevel; i++)
tab += "\t"; tab += "\t";
@ -957,20 +954,16 @@ Error DocData::save_classes(const String &p_default_path, const Map<String, Stri
header += ">"; header += ">";
_write_string(f, 0, header); _write_string(f, 0, header);
_write_string(f, 1, "<brief_description>"); _write_string(f, 1, "<brief_description>");
if (c.brief_description != "") _write_string(f, 2, c.brief_description.strip_edges().xml_escape());
_write_string(f, 2, c.brief_description.xml_escape());
_write_string(f, 1, "</brief_description>"); _write_string(f, 1, "</brief_description>");
_write_string(f, 1, "<description>"); _write_string(f, 1, "<description>");
if (c.description != "") _write_string(f, 2, c.description.strip_edges().xml_escape());
_write_string(f, 2, c.description.xml_escape());
_write_string(f, 1, "</description>"); _write_string(f, 1, "</description>");
_write_string(f, 1, "<tutorials>"); _write_string(f, 1, "<tutorials>");
if (c.tutorials != "") _write_string(f, 2, c.tutorials.strip_edges().xml_escape());
_write_string(f, 2, c.tutorials.xml_escape());
_write_string(f, 1, "</tutorials>"); _write_string(f, 1, "</tutorials>");
_write_string(f, 1, "<demos>"); _write_string(f, 1, "<demos>");
if (c.demos != "") _write_string(f, 2, c.demos.strip_edges().xml_escape());
_write_string(f, 2, c.demos.xml_escape());
_write_string(f, 1, "</demos>"); _write_string(f, 1, "</demos>");
_write_string(f, 1, "<methods>"); _write_string(f, 1, "<methods>");
@ -1014,8 +1007,7 @@ Error DocData::save_classes(const String &p_default_path, const Map<String, Stri
} }
_write_string(f, 3, "<description>"); _write_string(f, 3, "<description>");
if (m.description != "") _write_string(f, 4, m.description.strip_edges().xml_escape());
_write_string(f, 4, m.description.xml_escape());
_write_string(f, 3, "</description>"); _write_string(f, 3, "</description>");
_write_string(f, 2, "</method>"); _write_string(f, 2, "</method>");
@ -1036,8 +1028,7 @@ Error DocData::save_classes(const String &p_default_path, const Map<String, Stri
} }
PropertyDoc &p = c.properties[i]; PropertyDoc &p = c.properties[i];
_write_string(f, 2, "<member name=\"" + p.name + "\" type=\"" + p.type + "\" setter=\"" + p.setter + "\" getter=\"" + p.getter + "\"" + enum_text + ">"); _write_string(f, 2, "<member name=\"" + p.name + "\" type=\"" + p.type + "\" setter=\"" + p.setter + "\" getter=\"" + p.getter + "\"" + enum_text + ">");
if (p.description != "") _write_string(f, 3, p.description.strip_edges().xml_escape());
_write_string(f, 3, p.description.xml_escape());
_write_string(f, 2, "</member>"); _write_string(f, 2, "</member>");
} }
_write_string(f, 1, "</members>"); _write_string(f, 1, "</members>");
@ -1060,8 +1051,7 @@ Error DocData::save_classes(const String &p_default_path, const Map<String, Stri
} }
_write_string(f, 3, "<description>"); _write_string(f, 3, "<description>");
if (m.description != "") _write_string(f, 4, m.description.strip_edges().xml_escape());
_write_string(f, 4, m.description.xml_escape());
_write_string(f, 3, "</description>"); _write_string(f, 3, "</description>");
_write_string(f, 2, "</signal>"); _write_string(f, 2, "</signal>");
@ -1080,8 +1070,7 @@ Error DocData::save_classes(const String &p_default_path, const Map<String, Stri
} else { } else {
_write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\" enum=\"" + k.enumeration + "\">"); _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\" enum=\"" + k.enumeration + "\">");
} }
if (k.description != "") _write_string(f, 3, k.description.strip_edges().xml_escape());
_write_string(f, 3, k.description.xml_escape());
_write_string(f, 2, "</constant>"); _write_string(f, 2, "</constant>");
} }

View File

@ -1478,9 +1478,10 @@ static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt) {
Color font_color_hl = p_rt->get_color("headline_color", "EditorHelp"); Color font_color_hl = p_rt->get_color("headline_color", "EditorHelp");
Color link_color = p_rt->get_color("accent_color", "Editor").linear_interpolate(font_color_hl, 0.8); Color link_color = p_rt->get_color("accent_color", "Editor").linear_interpolate(font_color_hl, 0.8);
String bbcode = p_bbcode.replace("\t", " ").replace("\r", " ").strip_edges(); String bbcode = p_bbcode.dedent().replace("\t", "").replace("\r", "").strip_edges();
List<String> tag_stack; List<String> tag_stack;
bool code_tag = false;
int pos = 0; int pos = 0;
while (pos < bbcode.length()) { while (pos < bbcode.length()) {
@ -1491,7 +1492,10 @@ static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt) {
brk_pos = bbcode.length(); brk_pos = bbcode.length();
if (brk_pos > pos) { if (brk_pos > pos) {
p_rt->add_text(bbcode.substr(pos, brk_pos - pos)); String text = bbcode.substr(pos, brk_pos - pos);
if (!code_tag)
text = text.replace("\n", "\n\n");
p_rt->add_text(text);
} }
if (brk_pos == bbcode.length()) if (brk_pos == bbcode.length())
@ -1500,7 +1504,11 @@ static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt) {
int brk_end = bbcode.find("]", brk_pos + 1); int brk_end = bbcode.find("]", brk_pos + 1);
if (brk_end == -1) { if (brk_end == -1) {
p_rt->add_text(bbcode.substr(brk_pos, bbcode.length() - brk_pos));
String text = bbcode.substr(brk_pos, bbcode.length() - brk_pos);
if (!code_tag)
text = text.replace("\n", "\n\n");
p_rt->add_text(text);
break; break;
} }
@ -1509,20 +1517,23 @@ static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt) {
if (tag.begins_with("/")) { if (tag.begins_with("/")) {
bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length()); bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
if (tag_stack.size()) {
}
if (!tag_ok) { if (!tag_ok) {
p_rt->add_text("["); p_rt->add_text("[");
pos++; pos = brk_pos + 1;
continue; continue;
} }
tag_stack.pop_front(); tag_stack.pop_front();
pos = brk_end + 1; pos = brk_end + 1;
code_tag = false;
if (tag != "/img") if (tag != "/img")
p_rt->pop(); p_rt->pop();
} else if (code_tag) {
p_rt->add_text("[");
pos = brk_pos + 1;
} else if (tag.begins_with("method ")) { } else if (tag.begins_with("method ")) {
@ -1559,6 +1570,7 @@ static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt) {
//use monospace font //use monospace font
p_rt->push_font(doc_code_font); p_rt->push_font(doc_code_font);
code_tag = true;
pos = brk_end + 1; pos = brk_end + 1;
tag_stack.push_front(tag); tag_stack.push_front(tag);
} else if (tag == "center") { } else if (tag == "center") {