どのように私は、Pythonでマークダウンをロードする


パッケージを使用eyeseast/python-frontmatter ファイルをフロントエンドで読み込みます.その構造的フロントエンド(YAML、JSON、またはTOML)でファイルを読み込むことができる便利なパッケージです.

インストール


Ppyi上にあるので、PIPで仮想環境にインストールできます.
python -m pip install python-frontmatter

🙋 前線は何か


FrontMatterは、プレーンテキストファイルにメタデータを追加する便利な方法です.MarkdownでYAML FrontMatterを持つのは全く一般的です.私のブログのポストのすべては、ポスト日付、タグ、タイトル、およびテンプレートなどのポストメタデータを与えるためにYAML FrontMatterを持っています.dev . toは、また、MarkdownとYAML FrontMatterでそのポストの全てを構築する人気の開発者ブログ・プラットホームです.

例を見ましょう


あなたが私のサイトで読んでいるこのポストのための正確なフロントエンドは、ここにあります.
---
date: 2022-03-24 03:18:48.631729
templateKey: til
title: How I load Markdown in Python
tags:
  - linux
  - python

---

This is where the markdown content for the post goes.

それで、それはYAMLです


YAMLは最も通訳ですがpython-frontmatter サポートHandlers TOMLとJSONのために.
あなたがYAMLの例の良いセットを望むならばlearnxinyminutes つのページの例の素晴らしいセットがあります.

PythonでYAML frontmatterをロードする方法


以下はこのポストをPythonにロードする方法ですpython-frontmatter .
import frontmatter inspect(frontmatter.load("pages/til/python-frontmatter.md"))
私たちはrich Postオブジェクトを検査して、すべての内容を確認します.
 inspect(frontmatter.load("pages/til/python-frontmatter.md"))
╭────────────────────────────────────────────────────────── <class 'frontmatter.Post'> ───────────────────────────────────────────────────────────╮
│ A post contains content and metadata from Front Matter. This is what gets                                                                       │
│ returned by :py:func:`load <frontmatter.load>` and :py:func:`loads <frontmatter.loads>`.                                                        │
│ Passing this to :py:func:`dump <frontmatter.dump>` or :py:func:`dumps <frontmatter.dumps>`                                                      │
│ will turn it back into text.                                                                                                                    │
│                                                                                                                                                 │
│ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│ │ <frontmatter.Post object at 0x7f03c4c23ca0>                                                                                                 │ │
│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                                                                                 │
│  content = "I use a package\n[eyeseast/python-frontmatter](https://github.com/eyeseast/python-frontmatter)\nto load files with frontmatter in   │
│            them.  Its a handy package that allows you to\nload files with structured frontmatter (yaml, json, or toml).\n\n## Install\n\nIt's   
            on pypi, so you can install it into your virtual environment with pip.\n\n```
{% endraw %}
bash\npython -m pip install                             
            python-frontmatter\n
{% raw %}
```\n\n## 🙋 What's Frontmatter\n\nFrontmatter is a handy way to add metadata to your plain text files.          │
            It's\nquite common to have yaml frontmatter in markdown.  All of my blog posts have\nyaml frontmatter to give the post metadata such │
│            as post date, tags, title, and\ntemplate.  dev.to is a popular developer blogging platform that also builds all\nof its posts with   │
│            markdown and yaml frontmatter.\n\n## Let's see an example\n\nHere is the exact frontmatter for this post you are reading on my       
            site.\n\n```
{% endraw %}
markdown\n---\ndate: 2022-03-24 03:18:48.631729\ntemplateKey: til\ntitle: How I load Markdown in Python\ntags:\n  -      
            linux\n  - python\n\n---\n\nThis is where the markdown content for the post goes.\n
{% raw %}
```\n\n## So it's yaml\n\nyaml is the most        │
            commmon, but\n[eyeseast/python-frontmatter](https://github.com/eyeseast/python-frontmatter)\nalso                                    
            supports\n[Handlers](https://python-frontmatter.readthedocs.io/en/latest/handlers.html?highlight=toml#module-frontmatter.default_ha… │
            toml and json.\n\nIf you want a good set of examples of yaml\n[learnxinyminutes](https://learnxinyminutes.com/docs/yaml/) has a      
            fantastic set\nof examples in one page.\n\n## How to load yaml frontmatter in python"                                                │
  handler = <frontmatter.default_handlers.YAMLHandler object at 0x7f03bffbd910>                                                                  
 metadata = {                                                                                                                                    
                'date': datetime.datetime(2022, 3, 24, 3, 18, 48, 631729),                                                                       
                'templateKey': 'til',                                                                                                            
                'title': 'How I load Markdown in Python',                                                                                        
                'tags': ['linux', 'python', 'python']                                                                                            
            }                                                                                                                                    
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

my personal site handled this a bit better than dev.to https://waylonwalker.com/til/python-frontmatter/#how-to-load-yaml-frontmatter-in-python


メタデータの取得


あなただけのように、郵便番号からの投稿メタデータからアイテムを得ることができます.
post = frontmatter.load("pages/til/python-frontmatter.md") post['date']
# datetime.datetime(2022, 3, 24, 3, 18, 48, 631729)

post.get('date')
# datetime.datetime(2022, 3, 24, 3, 18, 48, 631729)

I have recently become fond of the .get method to give it an easy default value. 👉 full post


内容は内容


文書の内容は以下の通りです.content
post.content

リンク

  • python dict get
  • eyeseast/python-frontmatter
  • python-frontmatter
  • python-frontmatter Handlers
  • learnxinyminutes
  • python-frontmatter
  • rich