Skip to content

Commit

Permalink
Add ends column to parse output
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Apr 18, 2024
1 parent f812942 commit 32eff63
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
16 changes: 7 additions & 9 deletions R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,13 @@
#' way. The `id` column refers the tokens back to the original input text, the
#' `block` relates tokens together into blocks. Block elements increment the
#' block count when they are entered, and decrement it when they are excited.
#' Note the output doesn't specify when a block or span is excited. Spans cannot
#' have children (they are closed when a new span starts), so they are excited
#' right away. Blocks are excited when a new tag with the same indentation is
#' encountered. The `type` column provides the type of the block. The
#' `indentation` column provides the node level in the tree. A child block will
#' increase the indentation for as long as it is active. `ol_index` provides the
#' number associated with the ordered list element. `tight` indicates whether
#' the list is tight (i.e. it was provided with no empty lines between
#' list elements).
#' The `type` column provides the type of the block. The `indentation` column
#' provides the node level in the tree. A child block will increase the
#' indentation for as long as it is active. `ol_index` provides the number
#' associated with the ordered list element. `tight` indicates whether the list
#' is tight (i.e. it was provided with no empty lines between list elements).
#' The `ends` column indicate until which row in the output the tag is active
#' (i.e. the tag is closed after the row indicated by the value in this column).
#'
#' @export
#'
Expand Down
16 changes: 7 additions & 9 deletions man/marquee_parse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions src/marquee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ using namespace std::string_literals;
struct MARQUEE_DATA {
std::stack<cpp11::list> style_stack;
std::stack<std::string> type_stack;
std::vector<size_t> index_stack;
std::stack<int> offset_stack;
std::stack<bool> tight_stack;
cpp11::list_of<cpp11::list> defined_styles;
Expand All @@ -33,6 +34,7 @@ struct MARQUEE_DATA {
cpp11::writable::integers indent;
cpp11::writable::integers ol_index;
cpp11::writable::logicals tight;
cpp11::writable::integers until;
R_xlen_t current_id;
unsigned current_block;
unsigned current_indent;
Expand All @@ -41,6 +43,7 @@ struct MARQUEE_DATA {
MARQUEE_DATA(cpp11::list_of<cpp11::list> styles) :
style_stack(),
type_stack({""}),
index_stack(),
offset_stack({0}),
tight_stack({false}),
defined_styles(styles),
Expand All @@ -52,6 +55,7 @@ struct MARQUEE_DATA {
indent(),
ol_index(),
tight(),
until(),
current_id(0),
current_block(0),
current_indent(0),
Expand Down Expand Up @@ -151,6 +155,7 @@ inline cpp11::writable::list combine_styles(cpp11::list parent, cpp11::list def)

inline void push_info(MARQUEE_DATA* userdata, std::string type, bool block = false, bool tight = false, int offset = 1) {
userdata->type_stack.push(type);
userdata->index_stack.push_back(userdata->until.size());
cpp11::list style(userdata->defined_styles[type]);
if (userdata->style_stack.empty()) {
userdata->style_stack.push(style);
Expand Down Expand Up @@ -179,13 +184,19 @@ inline void init_text(MARQUEE_DATA* userdata) {
userdata->indent.push_back(userdata->current_indent);
userdata->ol_index.push_back(userdata->offset_stack.top());
userdata->tight.push_back(userdata->tight_stack.top());
userdata->until.push_back(userdata->until.size() + 1);
}

inline void pop_info(MARQUEE_DATA* userdata, std::string type, bool block = false) {
if (!userdata->style_stack.empty()) {
userdata->style_stack.pop();
}
userdata->type_stack.pop();
size_t cur_line = userdata->until.size();
for (size_t i = 0; i < userdata->index_stack.size(); ++i) {
userdata->until[userdata->index_stack[i]] = cur_line;
}
userdata->index_stack.pop_back();
if (block) {
userdata->current_indent--;
if (type != "li") {
Expand Down Expand Up @@ -364,9 +375,10 @@ cpp11::writable::list marquee_c(cpp11::strings text, cpp11::list_of<cpp11::list>
userdata.type,
userdata.indent,
userdata.ol_index,
userdata.tight
userdata.tight,
userdata.until
};
cpp11::writable::strings res_names = {"text", "id", "block", "type", "indentation", "ol_index", "tight"};
cpp11::writable::strings res_names = {"text", "id", "block", "type", "indentation", "ol_index", "tight", "ends"};

cpp11::list doc_style(userdata.style[0]);
double rem_size = REAL(doc_style[0])[0];
Expand Down

0 comments on commit 32eff63

Please sign in to comment.