rmarkdown to preserve line breaks in YAML header variable during export - r-markdown

I am exporting an rmarkdown file to odt, to html, and to pdf. But let's focus on the odt export first.
I want to have an address as part of the YAML header:
---
title: Test Multi
address:
First Name
Institute
Street
City
output:
odt_document:
template: default.opendocument
---
# Just a test document
With some text
This address should then be exported as part of the 'header'. Therefore, I have added the following snippet into the default odt-template (saved as default.opendocument):
$if(address)$
<text:p text:style-name="Author">$address$</text:p>
$endif$
But at the export the line breaks are lost:
Following this answer I tried pipes as in
---
title: Test Multi
address: |
| First Name
| Institute
| Street
| City
output:
odt_document:
template: default.opendocument
---
# Just a test document
With some text
But then, the address is missing completely from the odt.
So, my question is: How can I have the multi-line address from the YAML added to the export preserving the line breaks?
PS: I am using rmarkdown 1.12 and pandoc-2.7.3

There are two things going on.
As you discovered, you need address: | for YAML to not gobble up your line-breaks.
But the string is treated by pandoc as markdown and pandoc's markdown by default treats linebreaks inside a paragraph as spaces. But you can escape them:
address: |
First Name \
Institute \
Street \
City

Related

pandoc error: [WARNING] Could not parse YAML metadata: Unexpected 'd'

I am not sure why I get the following error message while converting markdown to PDF with a template:
[WARNING] Could not parse YAML metadata at line 10 column 1: Unexpected 'd'
Here is the command I use to build the document:
pandoc --template eisvogel -o output.pdf input.md
Here is the markdown file:
---
titlepage: true
title: Some title
author: [Author]
date: "2022-11-25"
colorlinks: true
disable-header-and-footer: true
---
# Quick Introduction
A famous guy once said:
The error occurs when I add the disable-header-and-footer: true. If this line is not here, then the Pandoc processes the document.
The space after the colon in the offending line is not a normal (ASCII) space but the Unicode character U+202F, "Narrow Non-Break Space". The YAML parser doesn't see a key-value pair but a single word because of that. Replace it with an ASCII space and everything should work.

How $ works in Ansible list

I'm new in ansible so I've been learning about how it works. And while I was doing so, I'm stumped in the RegEx because it doesn't work for some reason. Here's the code i'm working on.
---
- hosts: localhost
#get the targeted file
vars:
contents: "{{lookup ('file', '/home/sample/test.txt')}}"
tasks:
- name: Replacing strings
include_role:
name: ansible-replace-string
And then this the code for the role
- name: Removing the suffix .ear and ear
debug:
msg: "{{contents | map('regex_replace', '\\.ear', '') | list}}"
msg: "{{contents | map('regex_replace', 'ear$', '') | list}}"
I've read that $ is used if you want to capture particular string at the end. So I'm trying to capture .ear and ear. While .ear works fine, if I try and put the ear$ it breaks my code. If I remove the first msg, ear$ only works if there's no dot before it. For example:
byedear -> byed
web.statements.dear -> web.statements.dear
I'm not sure as to why it doesn't work for the second example. If needed further content here's a snippet of the test.txt i'm trying to play around with.
web.appstatementear
web.statements.ear
web.payments.ear
TmStatearments
hellodear
byedear
If anyone has ideas I'd be happy to hear them!

Set output_file in YAML

When I run render("test.Rmd") with these YAML parameters, the output is still test.docx and not teeeeeeeest.docx:
---
title: "mytitle"
author: "Me, myself and I"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: word_document
output_file: "teeeeeeeest.docx"
---
However, if I set the argument inside the render function (rmarkdown::render("test.Rmd", output_file = "teeeeeeeest.docx")), it works fine.
Is it possible to set the output_file argument inside the YAML header? Else, my purpose is to include the current date in the output file name when knitting with RStudio, how could I do that?
As shown here, it is possible to set the output file name inside the YAML by customizing the knit function within the YAML header. For this, you just need to add a 'knit:' hook in the header, such as in this example:
knit: (function(inputFile, encoding) {
rmarkdown::render(inputFile,
encoding=encoding,
output_file='new_file_name.html')) })

bash script - fetch only unique domains from email list to variable

I am new to bash and having problem understanding how to get this done.
Check all "To:" field email address domains and list all unique domains to a variable to compare it to from domain.
I get the "from address" domain by using
grep -m 1 "From: " filename | cut -f 2 -d '#' | cut -d ">" -f 1
when reading a mail stored in file filename.
For "to address" domain there can be multiple To: addresses and having multiple domains. I am not sure how to get unique domains from "to address field".
Example to address line will be like this:
To: user#domain.com, user2#domain.com,
User Name <sample#domaintest.com>, test#domainname.com
grep -m 1 "^To: " filename | cut -f 2 -d '#' | cut -d ">" -f 1
but there are different format of email. So I am not sure if grep is right or if I should search for awk or something.
I need to get the unique domain list from the "To:" field email address/addresses to a variable in bash script.
Desired output for above example:
domain.com,domaintest.com,domainname.com
If you are hellbent on doing this with line-oriented utilities, there is a utility formail in the Procmail distribution which can normalize things for you somewhat.
bash$ formail -czxTo: <<\==test==
> From: me <sender#example.com>
> To: you <first#example.org>,
> them <other#example.net>
> Subject: quick demo
>
> Very quick, innit.
> ==test==
first#example.org, other#example.net
So with that you have input which you can actually pass to grep or Awk ... or sed.
fromdom=$(formail -czxTo: <message | tr ',' '\n' | sed 's/.*#//')
The From: address will not be normalized by formail -czxFrom: but you can use a neat trick: make formail generate a reply back to the From: address, and then extract the To: header from that.
todoms=$(formail -rtzcxTo: <message | sed 's/.*#//')
In some more detail, -r says to create a new reply to whoever sent you message, and then we do -zcxTo: on that.
(The -t option may or may not do what you want. In this case, I would perhaps omit it. http://www.iki.fi/era/procmail/formail.html has (vague) documentation for what it does; see also the section just before http://www.iki.fi/era/procmail/mini-faq.html#group-writable and sorry for the clumsy link -- there doesn't seem to be a good page-internal anchor to link to.)
Email address normalization is tricky because there are so many variants to choose from.
From: Elvis Parsley <king#graceland.example.com>
From: king#graceland.example.com
From: "Parsley, Elvis" <king#graceland.example.com> (kill me, I have to use Outlook)
From: "quoted#string" <king#graceland.example.com> (wait, he is already dead)
To: This could fold <recipient#example.net>,
over multiple lines <another#example.org>
I would turn to a more capable language with proper support for parsing all of these formats. My choice would be Python, though you could probably also pull this off in a few lines of Ruby or Perl.
The email library was revamped in Python 3.6 so this assumes you have at least that version. The email.Headerregistry class which is new in 3.6 is particularly convenient here.
#!/usr/bin/env python3
from email.policy import default
from email import message_from_binary_file
import sys
if len(sys.argv) == 1:
sys.argv.append('-')
for arg in sys.argv[1:]:
if arg == '-':
handle = sys.stdin
else:
handle = open(arg, 'rb')
message = message_from_binary_file(handle, policy=default)
from_dom = message.get('From').address.domain
to_doms = set()
for addr in message.get('To').addresses:
dom = addr.domain
if dom == from_dom:
continue
to_doms.add(dom)
print(','.join([from_dom] + list(to_doms)))
if arg != '-':
handle.close()
This simply produces a comma-separated list of domain names; you might want to do the rest of the processing in Python too instead, or change this so that it prints something in a slightly different format.
You'd save this in a convenient place (say, /usr/local/bin/fromto) and mark it as executable (chmod 755 /usr/local/bin/fromto). Now you can call this from the shell like any other utility like grep.

Where to put header.tex to change page to Landscape Rmarkdown

I read here how to add what is needed to give me the ability to change my page to landscape:
enter link description here
However I have saved the header.tex file everyplace I could think of and I still end up getting the following error:
Called from: (function (e)
{
if (inherits(e, "shiny.silent.error"))
return()
handle <- getOption("shiny.error")
if (is.function(handle))
handle()
})(list(message = "pandoc document conversion failed with error 43",
call = NULL))
Browse[1]>
I know this is do to me typing \blandscape and \elandscape because the PDF works as soon as I delete those commands. What I need help with is figuring out where to save the header.tex file to so that Rmarkdown knows how to locate it. Thanks in advance for everyone's your help!
Follow up:
Taking scoa's advice about adding it to my header I edited the top of my Rmd file to the following. Unfortunately it still didn't work.
---
title: "Lake Report"
output: pdf_document
header-includes:
- \usepackage{lscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
---
\newpage
\blandscape
```{r Non-Seasonal MK results, echo=FALSE}
# MKanalysis <- reactive(HM_MK(data = NonSeasonalData(),n = 4))
kable(MKanalysis(), caption = "Non-Seasonal Trend Analysis")
```
The results of the Mann Kendall analysis....
\elandscape
Latex program/verision: pdfTex, Version 3.14159265-2.6-1.40.17
Save the header.tex in the same directory your Rmd file is. Also, rather than an external file, you could just add those commands in your yaml front matter, a more portable solution:
---
title: "Lake Report"
output: pdf_document
header-includes:
- \usepackage{lscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
---