Skip to content

Commit d14cbb6

Browse files
committed
support unordered lists
#feat fix #799
1 parent 3098343 commit d14cbb6

File tree

14 files changed

+288
-14
lines changed

14 files changed

+288
-14
lines changed

include/mrdocs/Metadata/Javadoc.hpp

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ enum class Kind
116116
heading,
117117
link,
118118
list_item,
119+
unordered_list,
119120
paragraph,
120121
param,
121122
returns,
@@ -178,6 +179,12 @@ enum class Parts
178179
//--------------------------------------------
179180

180181
/** This is a variant-like list element.
182+
183+
There are two types of nodes: text and block.
184+
185+
- The javadoc is a list of blocks.
186+
- A block contains a list of text elements.
187+
- A text element contains a string.
181188
*/
182189
struct MRDOCS_DECL
183190
Node
@@ -367,6 +374,8 @@ struct Copied : Reference
367374
/** A piece of block content
368375
369376
The top level is a list of blocks.
377+
378+
There are two types of blocks: headings and paragraphs
370379
*/
371380
struct MRDOCS_DECL
372381
Block : Node
@@ -447,7 +456,7 @@ struct Heading : Block
447456
bool equals(const Node& other) const noexcept override
448457
{
449458
return kind == other.kind &&
450-
*this == static_cast<const Heading&>(other);
459+
*this == dynamic_cast<const Heading&>(other);
451460
}
452461
};
453462

@@ -494,7 +503,7 @@ struct Brief : Paragraph
494503
bool equals(const Node& other) const noexcept override
495504
{
496505
return kind == other.kind &&
497-
*this == static_cast<const Brief&>(other);
506+
*this == dynamic_cast<const Brief&>(other);
498507
}
499508
};
500509

@@ -520,7 +529,7 @@ struct Admonition : Paragraph
520529
bool equals(const Node& other) const noexcept override
521530
{
522531
return kind == other.kind &&
523-
*this == static_cast<const Admonition&>(other);
532+
*this == dynamic_cast<const Admonition&>(other);
524533
}
525534
};
526535

@@ -542,7 +551,7 @@ struct Code : Paragraph
542551
bool equals(const Node& other) const noexcept override
543552
{
544553
return kind == other.kind &&
545-
*this == static_cast<const Code&>(other);
554+
*this == dynamic_cast<const Code&>(other);
546555
}
547556
};
548557

@@ -563,7 +572,31 @@ struct ListItem : Paragraph
563572
bool equals(const Node& other) const noexcept override
564573
{
565574
return kind == other.kind &&
566-
*this == static_cast<const ListItem&>(other);
575+
*this == dynamic_cast<const ListItem&>(other);
576+
}
577+
};
578+
579+
/** A list of list items
580+
*/
581+
struct UnorderedList : Paragraph
582+
{
583+
static constexpr Kind static_kind = Kind::unordered_list;
584+
585+
// Vector of list items
586+
List<ListItem> items;
587+
588+
UnorderedList()
589+
: Paragraph(Kind::unordered_list)
590+
{
591+
}
592+
593+
bool operator==(const UnorderedList&)
594+
const noexcept = default;
595+
596+
bool equals(const Node& other) const noexcept override
597+
{
598+
return kind == other.kind &&
599+
*this == dynamic_cast<const UnorderedList&>(other);
567600
}
568601
};
569602

@@ -784,6 +817,8 @@ visit(
784817
return f.template operator()<Copied>(std::forward<Args>(args)...);
785818
case Kind::list_item:
786819
return f.template operator()<ListItem>(std::forward<Args>(args)...);
820+
case Kind::unordered_list:
821+
return f.template operator()<UnorderedList>(std::forward<Args>(args)...);
787822
case Kind::paragraph:
788823
return f.template operator()<Paragraph>(std::forward<Args>(args)...);
789824
case Kind::param:
@@ -851,6 +886,8 @@ visit(
851886
return visitor.template visit<Copied>();
852887
case Kind::list_item:
853888
return visitor.template visit<ListItem>();
889+
case Kind::unordered_list:
890+
return visitor.template visit<UnorderedList>();
854891
case Kind::param:
855892
return visitor.template visit<Param>();
856893
case Kind::returns:
@@ -917,12 +954,11 @@ class Corpus;
917954

918955
/** A processed Doxygen-style comment attached to a declaration.
919956
*/
920-
class MRDOCS_DECL
957+
struct MRDOCS_DECL
921958
Javadoc
922959
{
923960
doc::List<doc::Block> blocks_;
924961

925-
public:
926962
/** Constructor.
927963
*/
928964
MRDOCS_DECL

include/mrdocs/MetadataFwd.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct Type##Info;
4141

4242
struct BaseInfo;
4343
struct Info;
44-
class Javadoc;
44+
struct Javadoc;
4545
struct Location;
4646
struct NameInfo;
4747
struct Param;

mrdocs.rnc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,15 @@ grammar
386386

387387
BlockNode = (
388388
Admonition | Brief | Code | Heading | ListItem |
389-
Paragraph | Param | Returns | TParam | Throws |
390-
See | Precondition | Postcondition | Details)
389+
UnorderedList | Paragraph | Param | Returns | TParam |
390+
Throws | See | Precondition | Postcondition | Details)
391391

392392
Admonition = Paragraph
393393
Brief = element brief { TextNode * }
394394
Code = element code { TextNode* }
395395
Heading = element head { text }
396396
ListItem = element listitem { TextNode* }
397+
UnorderedList = element unorderedlist { ListItem * }
397398
Paragraph = element para {
398399
attribute class { text } ?,
399400
TextNode * }

src/lib/AST/ParseJavadoc.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#define MRDOCS_COMMENT_TRACE(D, C)
4040
#else
4141

42-
# define MRDOCS_COMMENT_TRACE_MERGE_(a, b) a##b
42+
# define MRDOCS_COMMENT_TRACE_MERGE_(a, b) a## b
4343
# define MRDOCS_COMMENT_TRACE_LABEL_(a) MRDOCS_COMMENT_TRACE_MERGE_(comment_content_, a)
4444
# define MRDOCS_COMMENT_TRACE_UNIQUE_NAME MRDOCS_COMMENT_TRACE_LABEL_(__LINE__)
4545

@@ -564,7 +564,7 @@ visitChildren(
564564
{
565565
MRDOCS_COMMENT_TRACE(*it_, ctx_);
566566
visit(*it_);
567-
++it_; // must happen after
567+
++it_;
568568
}
569569

570570
if (!block_)
@@ -649,6 +649,35 @@ build()
649649
{
650650
MRDOCS_COMMENT_TRACE(FC_, ctx_);
651651
visit(FC_);
652+
653+
// Merge ListItems into UnorderedList
654+
auto& blocks = jd_.getBlocks();
655+
for (auto it = blocks.begin(); it != blocks.end(); ) {
656+
if ((*it)->kind == doc::Kind::list_item) {
657+
doc::UnorderedList ul;
658+
// Find last list item
659+
auto const begin = it;
660+
auto last = it;
661+
while (last != blocks.end() && (*last)->kind == doc::Kind::list_item) {
662+
++last;
663+
}
664+
// Move list items to ul.items
665+
ul.items.reserve(std::distance(it, last));
666+
for (auto li_it = begin; li_it != last; ++li_it) {
667+
std::unique_ptr<doc::Block> block = std::move(*li_it);
668+
MRDOCS_ASSERT(dynamic_cast<doc::ListItem*>(block.get()));
669+
doc::Block* raw_block_ptr = block.release();
670+
auto const raw_li_ptr = static_cast<doc::ListItem*>(raw_block_ptr);
671+
auto li = std::make_unique<doc::ListItem>(std::move(*raw_li_ptr));
672+
ul.items.emplace_back(std::move(li));
673+
}
674+
// Remove the list items and insert the ul
675+
it = blocks.erase(begin, last);
676+
it = blocks.insert(it, std::make_unique<doc::UnorderedList>(std::move(ul)));
677+
}
678+
++it;
679+
}
680+
652681
return std::move(jd_);
653682
}
654683

src/lib/Gen/adoc/DocVisitor.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,11 @@ operator()(
170170
doc::ListItem const& I) const
171171
{
172172
std::span children = I.children;
173-
if(children.empty())
173+
if (children.empty())
174+
{
174175
return;
175-
dest_.append("\n* ");
176+
}
177+
dest_.append("* ");
176178
bool non_empty = write(*children.front(), *this);
177179
for(auto const& child : children.subspan(1))
178180
{
@@ -185,6 +187,22 @@ operator()(
185187
dest_.push_back('\n');
186188
}
187189

190+
void
191+
DocVisitor::
192+
operator()(
193+
doc::UnorderedList const& I) const
194+
{
195+
if (I.items.empty())
196+
{
197+
return;
198+
}
199+
for(auto const& child : I.items)
200+
{
201+
operator()(*child);
202+
}
203+
dest_.push_back('\n');
204+
}
205+
188206
void
189207
DocVisitor::
190208
operator()(doc::Param const& I) const

src/lib/Gen/adoc/DocVisitor.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class DocVisitor
6868
void
6969
operator()(doc::ListItem const& I) const;
7070

71+
void
72+
operator()(doc::UnorderedList const& I) const;
73+
7174
void
7275
operator()(doc::Param const& I) const;
7376

src/lib/Gen/html/DocVisitor.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,23 @@ operator()(
211211
dest_.append("</li>\n");
212212
}
213213

214+
void
215+
DocVisitor::
216+
operator()(
217+
doc::UnorderedList const& I) const
218+
{
219+
if (I.items.empty())
220+
{
221+
return;
222+
}
223+
dest_.append("<ul>\n");
224+
for(auto const& child : I.items)
225+
{
226+
operator()(*child);
227+
}
228+
dest_.append("</ul>\n");
229+
}
230+
214231
void
215232
DocVisitor::
216233
operator()(doc::Param const& I) const

src/lib/Gen/html/DocVisitor.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class DocVisitor
6868
void
6969
operator()(doc::ListItem const& I) const;
7070

71+
void
72+
operator()(doc::UnorderedList const& I) const;
73+
7174
void
7275
operator()(doc::Param const& I) const;
7376

src/lib/Gen/xml/XMLWriter.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,9 @@ writeNode(
747747
case doc::Kind::list_item:
748748
writeListItem(dynamic_cast<doc::ListItem const&>(node));
749749
break;
750+
case doc::Kind::unordered_list:
751+
writeUnorderedList(dynamic_cast<doc::UnorderedList const&>(node));
752+
break;
750753
case doc::Kind::brief:
751754
writeBrief(dynamic_cast<doc::Brief const&>(node));
752755
break;
@@ -847,6 +850,16 @@ writeListItem(
847850
tags_.close("listitem");
848851
}
849852

853+
void
854+
XMLWriter::
855+
writeUnorderedList(
856+
doc::UnorderedList const& node)
857+
{
858+
tags_.open("unorderedlist");
859+
writeNodes(node.items);
860+
tags_.close("unorderedlist");
861+
}
862+
850863
void
851864
XMLWriter::
852865
writeBrief(

src/lib/Gen/xml/XMLWriter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class XMLWriter
8181
void writeHeading(doc::Heading const& node);
8282
void writeLink(doc::Link const& node);
8383
void writeListItem(doc::ListItem const& node);
84+
void writeUnorderedList(doc::UnorderedList const& node);
8485
void writeParagraph(doc::Paragraph const& node, llvm::StringRef tag = "");
8586
void writeJParam(doc::Param const& node);
8687
void writeReturns(doc::Returns const& node);

0 commit comments

Comments
 (0)