quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.77k stars 309 forks source link

Parsing of YAML options for ipynb output #851

Closed linogaliana closed 2 years ago

linogaliana commented 2 years ago

I experienced the fact that the title put in YAML options is not rendered in ipynb output format. I think that parsing the title, when it exists, into a level one header at the beginning of the file might be useful.

I came across the following python code to tweak the qmd file, it can be used as an inspiration if you are okay to implement this featuere (I know that you will not use python for such implementation but if it can help you I am happy to share):

import re

filename = "example.qmd"

with open(filename, 'r', encoding='utf-8') as f:
    text = f.readlines()
    new_text = "".join([line for line in text])

yaml, text = s.split('---\n', 2)[1:]
title_search = re.search('title: "(.*)"', s)
if title_search:
    title = "#" + title_search.group(1)
else:
    title = ""
new_md = title.replace('"', '').replace("'", "") + \
             "\n" + text

(Maybe there are other quarto YAML felds that could be parsed: I proposed title as a starting point)

jjallaire commented 2 years ago

This commit prepends a markdown cell with a title to the generated ipynb: https://github.com/quarto-dev/quarto-cli/commit/9d444502864f0c1ad03ba5b3300a56c5e1bab5cb

@dragonstyle I didn't include author/date b/c I'm guessing we actually require some custom processing there. Assigning to you so we can get that part right as well. This will be particularly important for people creating journal articles that have a companion notebook.

linogaliana commented 2 years ago

just tested, solution for title worked like a charm. Do you want me to close this issue or leave it open if you want to add other fields to the parser ?

jjallaire commented 2 years ago

Let's leave it open.

dragonstyle commented 2 years ago

This should be resolved with a reasonable treatment for title, subtitle, abstract, simple authors and affiliations, and dates. Thanks for reporting this!

9582f0de31fa2ff4468041df61eee6c8b7886dfd