aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ingmar Vanhassel <ingmar@exherbo.org> 2011-02-08 16:35:20 +0000
committerAvatar Ingmar Vanhassel <ingmar@exherbo.org> 2011-02-14 14:24:38 +0000
commit9ba3f44b20364464e20e7c2efdc89e5aefbcdda3 (patch)
tree7aab46d14a746538957706b1f37f72c8487ecacb
parent21e4ea2a1dd922dbab3e182b41aa0d4666b0057e (diff)
downloadpaludis-9ba3f44b20364464e20e7c2efdc89e5aefbcdda3.tar.gz
paludis-9ba3f44b20364464e20e7c2efdc89e5aefbcdda3.tar.xz
AsciidocWriter: Escape ' and *
'foo' underlines foo *foo* boldens foo We need \'foo' and \*foo* properly escape them. Only the first of the pair of asterisks and apostrophes needs escaping. Instead of tracking which one we are, we just escape the ones that are at the start of the word.
-rw-r--r--paludis/args/man.cc84
1 files changed, 71 insertions, 13 deletions
diff --git a/paludis/args/man.cc b/paludis/args/man.cc
index 0cf31e1..dc791b2 100644
--- a/paludis/args/man.cc
+++ b/paludis/args/man.cc
@@ -204,6 +204,51 @@ DocWriter::~DocWriter()
{
}
+namespace
+{
+ void escape_asciidoc(std::ostream & stream, const std::string & s)
+ {
+ // escape the n-th char unless n = 0 (start of string) or if n-1 = \s \t or \n
+ auto t(s.begin()), t_end(s.end());
+ char previous('\0');
+
+ if (t != t_end)
+ {
+ switch (*t)
+ {
+ case '\'':
+ stream << "\\'";
+ ++t;
+ break;
+ case '*':
+ stream << "\\*";
+ ++t;
+ break;
+ }
+ }
+
+ for ( ; t != t_end ; ++t)
+ {
+ switch (previous)
+ {
+ case ' ':
+ case '\n':
+ case '\t':
+ if ('\'' == *t or '*' == *t)
+ stream << '\\';
+ break;
+ // Escape '*/*' -> \'\*/*'
+ case '\'':
+ if ('*' == *t)
+ stream << '\\';
+ break;
+ }
+ stream << *t;
+ previous = *t;
+ }
+ }
+}
+
AsciidocWriter::AsciidocWriter(std::ostream & os) :
_os(os)
{
@@ -235,7 +280,9 @@ AsciidocWriter::start_usage_lines()
void
AsciidocWriter::usage_line(const std::string & name, const std::string & line)
{
- _os << "*" << name << " " << line << "*" << endl << endl;
+ _os << "*" << name << " ";
+ escape_asciidoc(_os, line);
+ _os << "*" << endl << endl;
}
void
@@ -243,13 +290,15 @@ AsciidocWriter::start_description(const std::string & description)
{
_os << "DESCRIPTION" << endl;
_os << "-----------" << endl;
- _os << description << endl;
+ escape_asciidoc(_os, description);
+ _os << endl;
}
void
AsciidocWriter::extra_description(const std::string & description)
{
- _os << description << endl << endl;
+ escape_asciidoc(_os, description);
+ _os << endl << endl;
}
void
@@ -272,7 +321,8 @@ AsciidocWriter::start_arg_group(const std::string & name, const std::string & de
{
_os << name << endl;
_os << std::string(name.size(), char('~')) << endl;
- _os << description << endl << endl;
+ escape_asciidoc(_os, description);
+ _os << endl << endl;
}
void
@@ -286,8 +336,9 @@ AsciidocWriter::arg_group_item(const char & short_name, const std::string & long
if (! negated_long_name.empty())
_os << " (" << "--" << negated_long_name << ")";
_os << "*::" << endl;
- _os << " " << description << endl;
- _os << endl;
+ _os << " ";
+ escape_asciidoc(_os, description);
+ _os << endl << endl;
}
void
@@ -306,14 +357,17 @@ AsciidocWriter::extra_arg_enum(const AllowedEnumArg & e, const std::string & def
if (e.short_name())
_os << " (" << std::string(1, e.short_name()) << ")";
_os << "*;;" << endl;
- _os << " " << e.description() << default_string << endl << endl;
+ _os << " ";
+ escape_asciidoc(_os, e.description());
+ _os << default_string << endl << endl;
}
void
AsciidocWriter::extra_arg_string_set(const std::string & first, const std::string & second)
{
_os << first << endl;
- _os << second << endl;
+ escape_asciidoc(_os, second);
+ _os << endl;
}
void
@@ -345,7 +399,8 @@ void
AsciidocWriter::environment_line(const std::string & first, const std::string & second)
{
_os << first << "::" << endl;
- _os << second << endl;
+ escape_asciidoc(_os, second);
+ _os << endl;
}
void
@@ -365,7 +420,8 @@ void
AsciidocWriter::example(const std::string & first, const std::string & second)
{
_os << first << "::" << endl;
- _os << second << endl;
+ escape_asciidoc(_os, second);
+ _os << endl;
}
void
@@ -384,7 +440,9 @@ AsciidocWriter::start_notes()
void
AsciidocWriter::note(const std::string & s)
{
- _os << "* " << s << endl;
+ _os << "* ";
+ escape_asciidoc(_os, s);
+ _os << endl;
}
void
@@ -410,7 +468,8 @@ AsciidocWriter::subsection(const std::string & title)
void
AsciidocWriter::paragraph(const std::string & text)
{
- _os << text << endl;
+ escape_asciidoc(_os, text);
+ _os << endl;
}
void
@@ -423,7 +482,6 @@ AsciidocWriter::start_see_alsos()
void
AsciidocWriter::see_also(const std::string & page, const int s, const bool)
{
- // FIXME: Bold page
_os << "*" << page << "*(" << s << ")" << endl;
}