Skip to content

Commit

Permalink
Fix #15
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed May 3, 2024
1 parent 05b284f commit ddf5ac9
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 17 deletions.
4 changes: 2 additions & 2 deletions R/cpp11.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by cpp11: do not edit by hand

marquee_c <- function(text, styles) {
.Call(`_marquee_marquee_c`, text, styles)
marquee_c <- function(text, styles, ignore_html) {
.Call(`_marquee_marquee_c`, text, styles, ignore_html)
}

place_bullets <- function(type, indent, string_is_empty, bullet_number, bullets) {
Expand Down
11 changes: 5 additions & 6 deletions R/grob.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#'
#' @param text Either a character vector or a `marquee_parsed` object as created
#' by [marquee_parse()]
#' @param style A style set such as [classic_style()] that defines how the text
#' should be rendered
#' @inheritParams marquee_parse
#' @param x,y The location of the markdown text in the graphics. If numeric it
#' will be converted to units using `default.units`
#' @param width The width of each markdown text. If numeric it will be converted
Expand Down Expand Up @@ -115,10 +114,10 @@
#' first or last line respectively.
#'
#' @export
marquee_grob <- function(text, style, x = 0, y = 1, width = NULL, default.units = "npc",
hjust = "left", vjust = "top", angle = 0, vp = NULL,
name = NULL) {
parsed <- if (!is_parsed(text)) marquee_parse(text, style) else text
marquee_grob <- function(text, style, ignore_html = TRUE, x = 0, y = 1,
width = NULL, default.units = "npc", hjust = "left",
vjust = "top", angle = 0, vp = NULL, name = NULL) {
parsed <- if (!is_parsed(text)) marquee_parse(text, style, ignore_html) else text

# Set bottom margin for tight list `li` elements to match the lineheight
is_tight <- parsed$type == "li" & parsed$tight
Expand Down
7 changes: 5 additions & 2 deletions R/parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' is valid markdown so there is no restrictions on the content
#' @param style A style set such as [classic_style()] that defines how the text
#' should be rendered
#' @param ignore_html Should HTML code be removed from the output
#'
#' @return A data frame describing the various tokens of the text and the style
#' to apply to them. The output is mainly meant for programmatic consumption
Expand Down Expand Up @@ -147,13 +148,15 @@
#' @examples
#' marquee_parse("# Header of the example\nSome body text", classic_style())
#'
marquee_parse <- function(text, style) {
marquee_parse <- function(text, style, ignore_html = TRUE) {
check_character(text)
if (!is_style_set(style)) {
cli::cli_abort("{.arg style} must be a style set")
}
style <- vctrs::vec_recycle(style, length(text))
parsed <- marquee_c(text, style)
ignore_html <- as.logical(vctrs::vec_recycle(ignore_html, length(text)))
ignore_html[is.na(ignore_html)] <- TRUE
parsed <- marquee_c(text, style, ignore_html)
# Remove terminal line end from code blocks
parsed$text[parsed$type == "cb"] <- sub("\n$", "", parsed$text[parsed$type == "cb"])
class(parsed) <- c("marquee_parsed", class(parsed))
Expand Down
3 changes: 3 additions & 0 deletions man/marquee_grob.Rd

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

4 changes: 3 additions & 1 deletion man/marquee_parse.Rd

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

8 changes: 4 additions & 4 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#include <R_ext/Visibility.h>

// marquee.cpp
cpp11::writable::list marquee_c(cpp11::strings text, cpp11::list_of<cpp11::list> styles);
extern "C" SEXP _marquee_marquee_c(SEXP text, SEXP styles) {
cpp11::writable::list marquee_c(cpp11::strings text, cpp11::list_of<cpp11::list> styles, cpp11::logicals ignore_html);
extern "C" SEXP _marquee_marquee_c(SEXP text, SEXP styles, SEXP ignore_html) {
BEGIN_CPP11
return cpp11::as_sexp(marquee_c(cpp11::as_cpp<cpp11::decay_t<cpp11::strings>>(text), cpp11::as_cpp<cpp11::decay_t<cpp11::list_of<cpp11::list>>>(styles)));
return cpp11::as_sexp(marquee_c(cpp11::as_cpp<cpp11::decay_t<cpp11::strings>>(text), cpp11::as_cpp<cpp11::decay_t<cpp11::list_of<cpp11::list>>>(styles), cpp11::as_cpp<cpp11::decay_t<cpp11::logicals>>(ignore_html)));
END_CPP11
}
// marquee.cpp
Expand All @@ -30,7 +30,7 @@ extern "C" SEXP _marquee_block_is_last(SEXP indentation, SEXP id) {
extern "C" {
static const R_CallMethodDef CallEntries[] = {
{"_marquee_block_is_last", (DL_FUNC) &_marquee_block_is_last, 2},
{"_marquee_marquee_c", (DL_FUNC) &_marquee_marquee_c, 2},
{"_marquee_marquee_c", (DL_FUNC) &_marquee_marquee_c, 3},
{"_marquee_place_bullets", (DL_FUNC) &_marquee_place_bullets, 5},
{NULL, NULL, 0}
};
Expand Down
6 changes: 4 additions & 2 deletions src/marquee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,19 +368,20 @@ static int text_callback(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size, vo
case MD_TEXT_BR: append_text(ud, "\n"); break;
case MD_TEXT_SOFTBR: append_text(ud, " "); break;
case MD_TEXT_ENTITY: append_text(ud, entity_to_unicode(std::string(text, size))); break;
case MD_TEXT_HTML: return 0;
default: append_text(ud, std::string(text, size)); break;
}

return 0;
}

[[cpp11::register]]
cpp11::writable::list marquee_c(cpp11::strings text, cpp11::list_of<cpp11::list> styles) {
cpp11::writable::list marquee_c(cpp11::strings text, cpp11::list_of<cpp11::list> styles, cpp11::logicals ignore_html) {
MARQUEE_DATA userdata(styles[0]);

MD_PARSER marquee_parser = {
0,
MD_FLAG_NOHTML | MD_FLAG_STRIKETHROUGH | MD_FLAG_UNDERLINE,
MD_FLAG_STRIKETHROUGH | MD_FLAG_UNDERLINE,
enter_block_callback,
leave_block_callback,
enter_span_callback,
Expand All @@ -393,6 +394,7 @@ cpp11::writable::list marquee_c(cpp11::strings text, cpp11::list_of<cpp11::list>
std::vector<double> rem_size;
for (R_xlen_t i = 0; i < text.size(); ++i) {
userdata.current_id = i + 1;
marquee_parser.flags = ignore_html[i] ? MD_FLAG_STRIKETHROUGH | MD_FLAG_UNDERLINE : MD_FLAG_NOHTML | MD_FLAG_STRIKETHROUGH | MD_FLAG_UNDERLINE;
userdata.style_stack = std::stack<cpp11::list>();
if (i != 0) userdata.defined_styles = create_style_map(styles[i]);
userdata.style_stack.push(userdata.defined_styles.find("base")->second);
Expand Down

0 comments on commit ddf5ac9

Please sign in to comment.