Improve Zeekygen output for long attribute expressions

Long inline-literals often don't wrap pleasantly in HTML renders and
we had a few &default or &deprecated attributes whose expressions
evaluate to a value long enough to warrant different formatting or
even truncation.
This commit is contained in:
Jon Siwek 2019-07-03 11:03:07 -07:00
parent c5c688054c
commit c03ef308c9
6 changed files with 43 additions and 15 deletions

View file

@ -1,4 +1,8 @@
2.6-566 | 2019-07-03 11:08:24 -0700
* Improve Zeekygen output for long attribute expressions (Jon Siwek, Corelight)
2.6-565 | 2019-07-03 09:32:34 -0700 2.6-565 | 2019-07-03 09:32:34 -0700
* GH-446: Deprecate rfb_event. (Johanna Amann, Corelight) * GH-446: Deprecate rfb_event. (Johanna Amann, Corelight)

View file

@ -1 +1 @@
2.6-565 2.6-566

2
doc

@ -1 +1 @@
Subproject commit be223b7e2fe9e2cfcfe8df4d594a6255276c6317 Subproject commit 80c0cb3d9db1cea6661b3d3c79d17e3b67533507

View file

@ -45,8 +45,33 @@ void Attr::Describe(ODesc* d) const
} }
} }
void Attr::DescribeReST(ODesc* d) const void Attr::DescribeReST(ODesc* d, bool shorten) const
{ {
auto add_long_expr_string = [](ODesc* d, const std::string& s, bool shorten)
{
constexpr auto max_expr_chars = 32;
constexpr auto shortened_expr = "*...*";
if ( s.size() > max_expr_chars )
{
if ( shorten )
d->Add(shortened_expr);
else
{
// Long inline-literals likely won't wrap well in HTML render
d->Add("*");
d->Add(s);
d->Add("*");
}
}
else
{
d->Add("``");
d->Add(s);
d->Add("``");
}
};
d->Add(":zeek:attr:`"); d->Add(":zeek:attr:`");
AddTag(d); AddTag(d);
d->Add("`"); d->Add("`");
@ -57,7 +82,6 @@ void Attr::DescribeReST(ODesc* d) const
d->Add("="); d->Add("=");
d->SP(); d->SP();
if ( expr->Tag() == EXPR_NAME ) if ( expr->Tag() == EXPR_NAME )
{ {
d->Add(":zeek:see:`"); d->Add(":zeek:see:`");
@ -74,14 +98,15 @@ void Attr::DescribeReST(ODesc* d) const
else if ( expr->Tag() == EXPR_CONST ) else if ( expr->Tag() == EXPR_CONST )
{ {
d->Add("``"); ODesc dd;
expr->Describe(d); dd.SetQuotes(1);
d->Add("``"); expr->Describe(&dd);
string s = dd.Description();
add_long_expr_string(d, s, shorten);
} }
else else
{ {
d->Add("``");
Val* v = expr->Eval(0); Val* v = expr->Eval(0);
ODesc dd; ODesc dd;
v->Describe(&dd); v->Describe(&dd);
@ -92,8 +117,7 @@ void Attr::DescribeReST(ODesc* d) const
if ( s[i] == '\n' ) if ( s[i] == '\n' )
s[i] = ' '; s[i] = ' ';
d->Add(s); add_long_expr_string(d, s, shorten);
d->Add("``");
} }
} }
} }
@ -211,14 +235,14 @@ void Attributes::Describe(ODesc* d) const
} }
} }
void Attributes::DescribeReST(ODesc* d) const void Attributes::DescribeReST(ODesc* d, bool shorten) const
{ {
loop_over_list(*attrs, i) loop_over_list(*attrs, i)
{ {
if ( i > 0 ) if ( i > 0 )
d->Add(" "); d->Add(" ");
(*attrs)[i]->DescribeReST(d); (*attrs)[i]->DescribeReST(d, shorten);
} }
} }

View file

@ -51,7 +51,7 @@ public:
{ return tag == ATTR_REDEF || tag == ATTR_OPTIONAL; } { return tag == ATTR_REDEF || tag == ATTR_OPTIONAL; }
void Describe(ODesc* d) const override; void Describe(ODesc* d) const override;
void DescribeReST(ODesc* d) const; void DescribeReST(ODesc* d, bool shorten = false) const;
bool operator==(const Attr& other) const bool operator==(const Attr& other) const
{ {
@ -88,7 +88,7 @@ public:
void RemoveAttr(attr_tag t); void RemoveAttr(attr_tag t);
void Describe(ODesc* d) const override; void Describe(ODesc* d) const override;
void DescribeReST(ODesc* d) const; void DescribeReST(ODesc* d, bool shorten = false) const;
attr_list* Attrs() { return attrs; } attr_list* Attrs() { return attrs; }

View file

@ -412,7 +412,7 @@ void ID::DescribeReSTShort(ODesc* d) const
if ( attrs ) if ( attrs )
{ {
d->SP(); d->SP();
attrs->DescribeReST(d); attrs->DescribeReST(d, true);
} }
} }