xaringan flair character strings with underscores

Hi all! I am trying to use the flair package in xaringan slides to highlight text strings. This is adapated from Danielle Navarro's slides source code.

I am having trouble with the regex to highlight patterns. Any help would be much appreciated!

The four compenents of the string that I would like to be able to pull out are:

[prefix]_[stem]_[description].[ext]

I can get the prefix and extention, but I am struggling with stem and description. I'm not sure if it is the regex or the flair, but in some cirumstances the underscores are rendering as italicized text.

Incorrect rendering of stem:

Description I can get when specificing exact text strings but not with regex patterns.

Here is the full demo .Rmd:

---
title: "Flair demo"
output:
  xaringan::moon_reader:
    lib_dir: libs
    nature:
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
---

---

```{r, warning=FALSE, message=FALSE}
library(tidyverse)
library(flair)
```

--- 


---

# text

```{r filenames, include=FALSE, results='hide'}
"2021-01-01_stem1_description1.xlsx"
"2021-01-01_stem1_description2.xlsx"
"2022-03-15_longstem1.pdf"
"2022-03-15_longstem1_description1.pdf"
```

---

# flair prefix

```{r, echo=FALSE, results='asis'}
decorate_chunk("filenames") %>% 
  flair_rx('"[^_\n]+') %>% 
  flair::knit_print.with_flair()
```

---

# flair stem attempts 

```{r, echo=FALSE, results='asis'}
decorate_chunk("filenames") %>% 
  flair_rx("_.*_") %>%
  flair::knit_print.with_flair()
```

```{r, echo=FALSE, results='asis'}
decorate_chunk("filenames") %>% 
  flair_rx("stem1") %>% 
  flair_rx("longstem1") %>% 
  flair::knit_print.with_flair()
```


---

# flair description attempts 

```{r, echo=FALSE, results='asis'}
decorate_chunk("filenames") %>% 
  flair_rx("_[^.]*") %>% 
  flair::knit_print.with_flair()
```

```{r, echo=FALSE, results='asis'}
decorate_chunk("filenames") %>% 
  flair_rx("_description1") %>% 
  flair_rx("_description2") %>% 
  flair::knit_print.with_flair()
```


---

# flair extension  

```{r, echo=FALSE, results='asis'}
decorate_chunk("filenames") %>% 
  flair_rx("\\..*") %>% 
  flair::knit_print.with_flair()
```

It looks like flair detects html tags for formatting.
So it sees fun underscores and decides that it should be italicized and therefore your text between the underscores is italics and not highlighted! (also the underscores are removed).

One possible fix would be to add some escape characters to the filenames or otherwise temporarily replace them with something other than underscores.

For the stem regex, here is an overly complicated solution:
(?<=_)[a-z0-9]+?(?=[_\\.])
or _[^_\.]*[_\.]
This should non-greedy look for alphanumeric stems that are after underscore and before an underscore or period.

As pointed out - this does not solve the known issue with italics :grimacing:

1 Like

It seems like a known issue as I found this

something to fix in flair ?

2 Likes

Thank you, Alice! However, if I am correctly executing the suggestion as intended, the underscores still render italics.

Thank you for pointing me to this!

1 Like

Yeah, this seems really super frustrating! Hopefully, it can be fixed in {flair}!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.