Skip to content

Instantly share code, notes, and snippets.

@rdhyee
Created September 22, 2023 00:33
Show Gist options
  • Save rdhyee/5a960db8e74af154535553b154c0df9d to your computer and use it in GitHub Desktop.
Save rdhyee/5a960db8e74af154535553b154c0df9d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path as P\n",
"import pypandoc\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# https://stackoverflow.com/a/67029719\n",
"\n",
"from IPython import get_ipython\n",
"from IPython.core.magic import register_cell_magic\n",
"\n",
"ipython = get_ipython()\n",
"\n",
"@register_cell_magic\n",
"def pybash(line, cell):\n",
" ipython.run_cell_magic('bash', '', cell.format(**globals()))"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"import subprocess\n",
"\n",
"def run_cmd(*args, shell=True, cwd=None):\n",
" r = subprocess.run(*args, shell=shell, capture_output=True, cwd=cwd)\n",
" return r\n",
"\n",
"\n",
"def pandoc_formats():\n",
"\n",
" cmd = 'pandoc --list-input-formats'\n",
" r = run_cmd(cmd, shell=True, cwd=P.cwd())\n",
" input_formats = set (r.stdout.decode('utf-8').split('\\n'))\n",
"\n",
" cmd = 'pandoc --list-output-formats'\n",
" r = run_cmd(cmd, shell=True, cwd=P.cwd())\n",
" output_formats = set (r.stdout.decode('utf-8').split('\\n'))\n",
"\n",
" return {'input': input_formats, 'output': output_formats}\n",
"\n",
"\n",
"def extensions_for_format(fmt):\n",
" cmd = f'pandoc --list-extensions {fmt}'\n",
" r = run_cmd(cmd, shell=True, cwd=P.cwd())\n",
" # partition the extensions into two sets based by default enabling or disabling\n",
" enabled = set()\n",
" disabled = set()\n",
" for e in r.stdout.decode('utf-8').split('\\n'):\n",
" if e.startswith('+'):\n",
" enabled.add(e[1:])\n",
" elif e.startswith('-'):\n",
" disabled.add(e[1:])\n",
" return {'enabled': enabled, 'disabled': disabled, 'all': enabled.union(disabled)}\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'enabled': {'all_symbols_escapable',\n",
" 'auto_identifiers',\n",
" 'backtick_code_blocks',\n",
" 'blank_before_blockquote',\n",
" 'blank_before_header',\n",
" 'bracketed_spans',\n",
" 'citations',\n",
" 'definition_lists',\n",
" 'escaped_line_breaks',\n",
" 'example_lists',\n",
" 'fancy_lists',\n",
" 'fenced_code_attributes',\n",
" 'fenced_code_blocks',\n",
" 'fenced_divs',\n",
" 'footnotes',\n",
" 'grid_tables',\n",
" 'header_attributes',\n",
" 'implicit_figures',\n",
" 'implicit_header_references',\n",
" 'inline_code_attributes',\n",
" 'inline_notes',\n",
" 'intraword_underscores',\n",
" 'latex_macros',\n",
" 'line_blocks',\n",
" 'link_attributes',\n",
" 'markdown_in_html_blocks',\n",
" 'multiline_tables',\n",
" 'native_divs',\n",
" 'native_spans',\n",
" 'pandoc_title_block',\n",
" 'pipe_tables',\n",
" 'raw_attribute',\n",
" 'raw_html',\n",
" 'raw_tex',\n",
" 'shortcut_reference_links',\n",
" 'simple_tables',\n",
" 'smart',\n",
" 'space_in_atx_header',\n",
" 'startnum',\n",
" 'strikeout',\n",
" 'subscript',\n",
" 'superscript',\n",
" 'table_captions',\n",
" 'task_lists',\n",
" 'tex_math_dollars',\n",
" 'yaml_metadata_block'},\n",
" 'disabled': {'abbreviations',\n",
" 'angle_brackets_escapable',\n",
" 'ascii_identifiers',\n",
" 'autolink_bare_uris',\n",
" 'compact_definition_lists',\n",
" 'east_asian_line_breaks',\n",
" 'emoji',\n",
" 'four_space_rule',\n",
" 'gfm_auto_identifiers',\n",
" 'gutenberg',\n",
" 'hard_line_breaks',\n",
" 'ignore_line_breaks',\n",
" 'lists_without_preceding_blankline',\n",
" 'literate_haskell',\n",
" 'mark',\n",
" 'markdown_attribute',\n",
" 'mmd_header_identifiers',\n",
" 'mmd_link_attributes',\n",
" 'mmd_title_block',\n",
" 'old_dashes',\n",
" 'rebase_relative_paths',\n",
" 'short_subsuperscripts',\n",
" 'spaced_reference_links',\n",
" 'tex_math_double_backslash',\n",
" 'tex_math_single_backslash',\n",
" 'wikilinks_title_after_pipe',\n",
" 'wikilinks_title_before_pipe'},\n",
" 'all': {'abbreviations',\n",
" 'all_symbols_escapable',\n",
" 'angle_brackets_escapable',\n",
" 'ascii_identifiers',\n",
" 'auto_identifiers',\n",
" 'autolink_bare_uris',\n",
" 'backtick_code_blocks',\n",
" 'blank_before_blockquote',\n",
" 'blank_before_header',\n",
" 'bracketed_spans',\n",
" 'citations',\n",
" 'compact_definition_lists',\n",
" 'definition_lists',\n",
" 'east_asian_line_breaks',\n",
" 'emoji',\n",
" 'escaped_line_breaks',\n",
" 'example_lists',\n",
" 'fancy_lists',\n",
" 'fenced_code_attributes',\n",
" 'fenced_code_blocks',\n",
" 'fenced_divs',\n",
" 'footnotes',\n",
" 'four_space_rule',\n",
" 'gfm_auto_identifiers',\n",
" 'grid_tables',\n",
" 'gutenberg',\n",
" 'hard_line_breaks',\n",
" 'header_attributes',\n",
" 'ignore_line_breaks',\n",
" 'implicit_figures',\n",
" 'implicit_header_references',\n",
" 'inline_code_attributes',\n",
" 'inline_notes',\n",
" 'intraword_underscores',\n",
" 'latex_macros',\n",
" 'line_blocks',\n",
" 'link_attributes',\n",
" 'lists_without_preceding_blankline',\n",
" 'literate_haskell',\n",
" 'mark',\n",
" 'markdown_attribute',\n",
" 'markdown_in_html_blocks',\n",
" 'mmd_header_identifiers',\n",
" 'mmd_link_attributes',\n",
" 'mmd_title_block',\n",
" 'multiline_tables',\n",
" 'native_divs',\n",
" 'native_spans',\n",
" 'old_dashes',\n",
" 'pandoc_title_block',\n",
" 'pipe_tables',\n",
" 'raw_attribute',\n",
" 'raw_html',\n",
" 'raw_tex',\n",
" 'rebase_relative_paths',\n",
" 'short_subsuperscripts',\n",
" 'shortcut_reference_links',\n",
" 'simple_tables',\n",
" 'smart',\n",
" 'space_in_atx_header',\n",
" 'spaced_reference_links',\n",
" 'startnum',\n",
" 'strikeout',\n",
" 'subscript',\n",
" 'superscript',\n",
" 'table_captions',\n",
" 'task_lists',\n",
" 'tex_math_dollars',\n",
" 'tex_math_double_backslash',\n",
" 'tex_math_single_backslash',\n",
" 'wikilinks_title_after_pipe',\n",
" 'wikilinks_title_before_pipe',\n",
" 'yaml_metadata_block'}}"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"extensions_for_format('markdown')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"md_showcase =P(\"/Users/raymondyee/obsidian/MainRY/Obsidian markdown showcase.md\")\n",
"md_showcase.exists()"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"%%pybash\n",
"\n",
"# pandoc -f markdown+task_lists+backtick_code_blocks -t html \"{md_showcase}\"\n",
"pandoc -f markdown+wikilinks_title_after_pipe+mark+lists_without_preceding_blankline+task_lists+backtick_code_blocks -t html \"{md_showcase}\" > /tmp/obsidian.html\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"biblatex\n",
"bibtex\n",
"commonmark\n",
"commonmark_x\n",
"creole\n",
"csljson\n",
"csv\n",
"docbook\n",
"docx\n",
"dokuwiki\n",
"endnotexml\n",
"epub\n",
"fb2\n",
"gfm\n",
"haddock\n",
"html\n",
"ipynb\n",
"jats\n",
"jira\n",
"json\n",
"latex\n",
"man\n",
"markdown\n",
"markdown_github\n",
"markdown_mmd\n",
"markdown_phpextra\n",
"markdown_strict\n",
"mediawiki\n",
"muse\n",
"native\n",
"odt\n",
"opml\n",
"org\n",
"ris\n",
"rst\n",
"rtf\n",
"t2t\n",
"textile\n",
"tikiwiki\n",
"tsv\n",
"twiki\n",
"typst\n",
"vimwiki\n"
]
}
],
"source": [
"%%pybash\n",
"\n",
"pandoc --list-input-formats"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-abbreviations\n",
"+all_symbols_escapable\n",
"-angle_brackets_escapable\n",
"-ascii_identifiers\n",
"+auto_identifiers\n",
"-autolink_bare_uris\n",
"+backtick_code_blocks\n",
"+blank_before_blockquote\n",
"+blank_before_header\n",
"+bracketed_spans\n",
"+citations\n",
"-compact_definition_lists\n",
"+definition_lists\n",
"-east_asian_line_breaks\n",
"-emoji\n",
"+escaped_line_breaks\n",
"+example_lists\n",
"+fancy_lists\n",
"+fenced_code_attributes\n",
"+fenced_code_blocks\n",
"+fenced_divs\n",
"+footnotes\n",
"-four_space_rule\n",
"-gfm_auto_identifiers\n",
"+grid_tables\n",
"-gutenberg\n",
"-hard_line_breaks\n",
"+header_attributes\n",
"-ignore_line_breaks\n",
"+implicit_figures\n",
"+implicit_header_references\n",
"+inline_code_attributes\n",
"+inline_notes\n",
"+intraword_underscores\n",
"+latex_macros\n",
"+line_blocks\n",
"+link_attributes\n",
"-lists_without_preceding_blankline\n",
"-literate_haskell\n",
"-mark\n",
"-markdown_attribute\n",
"+markdown_in_html_blocks\n",
"-mmd_header_identifiers\n",
"-mmd_link_attributes\n",
"-mmd_title_block\n",
"+multiline_tables\n",
"+native_divs\n",
"+native_spans\n",
"-old_dashes\n",
"+pandoc_title_block\n",
"+pipe_tables\n",
"+raw_attribute\n",
"+raw_html\n",
"+raw_tex\n",
"-rebase_relative_paths\n",
"-short_subsuperscripts\n",
"+shortcut_reference_links\n",
"+simple_tables\n",
"+smart\n",
"+space_in_atx_header\n",
"-spaced_reference_links\n",
"+startnum\n",
"+strikeout\n",
"+subscript\n",
"+superscript\n",
"+task_lists\n",
"+table_captions\n",
"+tex_math_dollars\n",
"-tex_math_double_backslash\n",
"-tex_math_single_backslash\n",
"-wikilinks_title_after_pipe\n",
"-wikilinks_title_before_pipe\n",
"+yaml_metadata_block\n"
]
}
],
"source": [
"%%pybash\n",
"\n",
"pandoc --list-extensions markdown"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "myenv-3.11.3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment