python-frontmatter 1.1.0__tar.gz → 1.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. {python-frontmatter-1.1.0/python_frontmatter.egg-info → python_frontmatter-1.3.0}/PKG-INFO +37 -17
  2. {python-frontmatter-1.1.0 → python_frontmatter-1.3.0}/README.md +14 -5
  3. {python-frontmatter-1.1.0 → python_frontmatter-1.3.0}/frontmatter/__init__.py +35 -23
  4. {python-frontmatter-1.1.0 → python_frontmatter-1.3.0}/frontmatter/default_handlers.py +9 -8
  5. python_frontmatter-1.3.0/frontmatter/py.typed +1 -0
  6. python_frontmatter-1.3.0/frontmatter/util.py +31 -0
  7. python_frontmatter-1.3.0/pyproject.toml +50 -0
  8. python-frontmatter-1.1.0/MANIFEST.in +0 -6
  9. python-frontmatter-1.1.0/PKG-INFO +0 -150
  10. python-frontmatter-1.1.0/frontmatter/util.py +0 -18
  11. python-frontmatter-1.1.0/python_frontmatter.egg-info/SOURCES.txt +0 -40
  12. python-frontmatter-1.1.0/python_frontmatter.egg-info/dependency_links.txt +0 -1
  13. python-frontmatter-1.1.0/python_frontmatter.egg-info/not-zip-safe +0 -1
  14. python-frontmatter-1.1.0/python_frontmatter.egg-info/requires.txt +0 -12
  15. python-frontmatter-1.1.0/python_frontmatter.egg-info/top_level.txt +0 -1
  16. python-frontmatter-1.1.0/setup.cfg +0 -4
  17. python-frontmatter-1.1.0/setup.py +0 -50
  18. python-frontmatter-1.1.0/tests/empty/empty-frontmatter.result.json +0 -3
  19. python-frontmatter-1.1.0/tests/empty/empty-frontmatter.txt +0 -4
  20. python-frontmatter-1.1.0/tests/empty/no-frontmatter.result.json +0 -3
  21. python-frontmatter-1.1.0/tests/empty/no-frontmatter.txt +0 -1
  22. python-frontmatter-1.1.0/tests/json/hello-json.md +0 -18
  23. python-frontmatter-1.1.0/tests/json/hello-json.result.json +0 -6
  24. python-frontmatter-1.1.0/tests/stub_tests.py +0 -21
  25. python-frontmatter-1.1.0/tests/test_docs.py +0 -17
  26. python-frontmatter-1.1.0/tests/test_files.py +0 -46
  27. python-frontmatter-1.1.0/tests/toml/hello-toml.md +0 -18
  28. python-frontmatter-1.1.0/tests/toml/hello-toml.result.json +0 -6
  29. python-frontmatter-1.1.0/tests/unit_test.py +0 -398
  30. python-frontmatter-1.1.0/tests/yaml/chinese.result.json +0 -5
  31. python-frontmatter-1.1.0/tests/yaml/chinese.txt +0 -6
  32. python-frontmatter-1.1.0/tests/yaml/extra-dash.result.json +0 -5
  33. python-frontmatter-1.1.0/tests/yaml/extra-dash.txt +0 -6
  34. python-frontmatter-1.1.0/tests/yaml/extra-space.result.json +0 -5
  35. python-frontmatter-1.1.0/tests/yaml/extra-space.txt +0 -6
  36. python-frontmatter-1.1.0/tests/yaml/hello-markdown.md +0 -18
  37. python-frontmatter-1.1.0/tests/yaml/hello-markdown.result.json +0 -6
  38. python-frontmatter-1.1.0/tests/yaml/hello-world.result.json +0 -5
  39. python-frontmatter-1.1.0/tests/yaml/hello-world.txt +0 -6
  40. python-frontmatter-1.1.0/tests/yaml/network-diagrams.md +0 -12
  41. python-frontmatter-1.1.0/tests/yaml/network-diagrams.result.json +0 -9
  42. python-frontmatter-1.1.0/tests/yaml/unpretty.md +0 -21
  43. python-frontmatter-1.1.0/tests/yaml/unpretty.result.json +0 -23
  44. {python-frontmatter-1.1.0 → python_frontmatter-1.3.0}/LICENSE +0 -0
  45. {python-frontmatter-1.1.0 → python_frontmatter-1.3.0}/frontmatter/conftest.py +0 -0
@@ -1,30 +1,41 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: python-frontmatter
3
- Version: 1.1.0
3
+ Version: 1.3.0
4
4
  Summary: Parse and manage posts with YAML (or other) frontmatter
5
- Home-page: https://github.com/eyeseast/python-frontmatter
6
- Author: Chris Amico
7
- Author-email: eyeseast@gmail.com
8
- License: MIT
9
5
  Keywords: frontmatter
6
+ Author: Chris Amico
7
+ Author-email: Chris Amico <eyeseast@gmail.com>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
10
  Classifier: Development Status :: 5 - Production/Stable
11
11
  Classifier: Intended Audience :: Developers
12
- Classifier: License :: OSI Approved :: MIT License
13
12
  Classifier: Natural Language :: English
14
13
  Classifier: Programming Language :: Python :: 3
15
- Classifier: Programming Language :: Python :: 3.8
16
- Classifier: Programming Language :: Python :: 3.9
17
14
  Classifier: Programming Language :: Python :: 3.10
18
15
  Classifier: Programming Language :: Python :: 3.11
19
16
  Classifier: Programming Language :: Python :: 3.12
20
- Description-Content-Type: text/markdown
21
- Provides-Extra: test
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Requires-Dist: pyyaml
19
+ Requires-Dist: sphinx ; extra == 'docs'
20
+ Requires-Dist: pytest ; extra == 'test'
21
+ Requires-Dist: toml ; extra == 'test'
22
+ Requires-Dist: pyaml ; extra == 'test'
23
+ Requires-Dist: mypy ; extra == 'test'
24
+ Requires-Dist: types-pyyaml ; extra == 'test'
25
+ Requires-Dist: types-toml ; extra == 'test'
26
+ Requires-Dist: toml ; extra == 'toml'
27
+ Requires-Python: >=3.10
28
+ Project-URL: repository, https://github.com/eyeseast/python-frontmatter
29
+ Project-URL: issues, https://github.com/eyeseast/python-frontmatter/issues
30
+ Project-URL: documentation, http://python-frontmatter.rtfd.io
22
31
  Provides-Extra: docs
23
- License-File: LICENSE
32
+ Provides-Extra: test
33
+ Provides-Extra: toml
34
+ Description-Content-Type: text/markdown
24
35
 
25
36
  # Python Frontmatter
26
37
 
27
- [Jekyll](http://jekyllrb.com/)-style YAML front matter offers a useful way to add arbitrary, structured metadata to text documents, regardless of type.
38
+ [Front matter](https://docs.github.com/en/contributing/writing-for-github-docs/using-yaml-frontmatter) offers a useful way to add arbitrary, structured metadata to text documents, regardless of type.
28
39
 
29
40
  This is a small package to load and parse files (or just text) with YAML (or JSON, TOML or other) front matter.
30
41
 
@@ -35,7 +46,16 @@ This is a small package to load and parse files (or just text) with YAML (or JSO
35
46
 
36
47
  ## Install:
37
48
 
38
- pip install python-frontmatter
49
+ ```shell
50
+ # with pip
51
+ pip install python-frontmatter
52
+
53
+ # or uv
54
+ uv add python-frontmatter
55
+
56
+ # local development, with uv
57
+ uv sync
58
+ ```
39
59
 
40
60
  ## Usage:
41
61
 
@@ -134,10 +154,10 @@ Well, hello there, world.
134
154
  Or write to a file (or file-like object):
135
155
 
136
156
  ```python
137
- >>> from io import BytesIO
138
- >>> f = BytesIO()
157
+ >>> from io import StringIO
158
+ >>> f = StringIO()
139
159
  >>> frontmatter.dump(post, f)
140
- >>> print(f.getvalue().decode('utf-8')) # doctest: +NORMALIZE_WHITESPACE
160
+ >>> print(f.getvalue()) # doctest: +NORMALIZE_WHITESPACE
141
161
  ---
142
162
  excerpt: tl;dr
143
163
  layout: post
@@ -1,6 +1,6 @@
1
1
  # Python Frontmatter
2
2
 
3
- [Jekyll](http://jekyllrb.com/)-style YAML front matter offers a useful way to add arbitrary, structured metadata to text documents, regardless of type.
3
+ [Front matter](https://docs.github.com/en/contributing/writing-for-github-docs/using-yaml-frontmatter) offers a useful way to add arbitrary, structured metadata to text documents, regardless of type.
4
4
 
5
5
  This is a small package to load and parse files (or just text) with YAML (or JSON, TOML or other) front matter.
6
6
 
@@ -11,7 +11,16 @@ This is a small package to load and parse files (or just text) with YAML (or JSO
11
11
 
12
12
  ## Install:
13
13
 
14
- pip install python-frontmatter
14
+ ```shell
15
+ # with pip
16
+ pip install python-frontmatter
17
+
18
+ # or uv
19
+ uv add python-frontmatter
20
+
21
+ # local development, with uv
22
+ uv sync
23
+ ```
15
24
 
16
25
  ## Usage:
17
26
 
@@ -110,10 +119,10 @@ Well, hello there, world.
110
119
  Or write to a file (or file-like object):
111
120
 
112
121
  ```python
113
- >>> from io import BytesIO
114
- >>> f = BytesIO()
122
+ >>> from io import StringIO
123
+ >>> f = StringIO()
115
124
  >>> frontmatter.dump(post, f)
116
- >>> print(f.getvalue().decode('utf-8')) # doctest: +NORMALIZE_WHITESPACE
125
+ >>> print(f.getvalue()) # doctest: +NORMALIZE_WHITESPACE
117
126
  ---
118
127
  excerpt: tl;dr
119
128
  layout: post
@@ -4,12 +4,13 @@ Python Frontmatter: Parse and manage posts with YAML frontmatter
4
4
  """
5
5
  from __future__ import annotations
6
6
 
7
- import codecs
8
7
  import io
9
- from typing import TYPE_CHECKING, Iterable
8
+ import pathlib
9
+ from os import PathLike
10
+ from typing import TYPE_CHECKING, Iterable, TextIO
10
11
 
11
- from .util import u
12
- from .default_handlers import YAMLHandler, JSONHandler, TOMLHandler
12
+ from .default_handlers import JSONHandler, TOMLHandler, YAMLHandler
13
+ from .util import can_open, is_readable, is_writable, u
13
14
 
14
15
 
15
16
  if TYPE_CHECKING:
@@ -96,7 +97,7 @@ def parse(
96
97
  return metadata, content.strip()
97
98
 
98
99
 
99
- def check(fd: str | io.IOBase, encoding: str = "utf-8") -> bool:
100
+ def check(fd: TextIO | PathLike[str] | str, encoding: str = "utf-8") -> bool:
100
101
  """
101
102
  Check if a file-like object or filename has a frontmatter,
102
103
  return True if exists, False otherwise.
@@ -109,13 +110,17 @@ def check(fd: str | io.IOBase, encoding: str = "utf-8") -> bool:
109
110
  True
110
111
 
111
112
  """
112
- if hasattr(fd, "read"):
113
+ if is_readable(fd):
113
114
  text = fd.read()
114
115
 
115
- else:
116
- with codecs.open(fd, "r", encoding) as f:
116
+ elif can_open(fd):
117
+ with open(fd, "r", encoding=encoding) as f:
117
118
  text = f.read()
118
119
 
120
+ else:
121
+ # no idea what we're dealing with
122
+ return False
123
+
119
124
  return checks(text, encoding)
120
125
 
121
126
 
@@ -138,7 +143,7 @@ def checks(text: str, encoding: str = "utf-8") -> bool:
138
143
 
139
144
 
140
145
  def load(
141
- fd: str | io.IOBase,
146
+ fd: str | io.IOBase | pathlib.Path,
142
147
  encoding: str = "utf-8",
143
148
  handler: BaseHandler | None = None,
144
149
  **defaults: object,
@@ -154,13 +159,16 @@ def load(
154
159
  ... post = frontmatter.load(f)
155
160
 
156
161
  """
157
- if hasattr(fd, "read"):
162
+ if is_readable(fd):
158
163
  text = fd.read()
159
164
 
160
- else:
161
- with codecs.open(fd, "r", encoding) as f:
165
+ elif can_open(fd):
166
+ with open(fd, "r", encoding=encoding) as f:
162
167
  text = f.read()
163
168
 
169
+ else:
170
+ raise ValueError(f"Cannot open filename using type {type(fd)}")
171
+
164
172
  handler = handler or detect_format(text, handlers)
165
173
  return loads(text, encoding, handler, **defaults)
166
174
 
@@ -188,7 +196,7 @@ def loads(
188
196
 
189
197
  def dump(
190
198
  post: Post,
191
- fd: str | io.IOBase,
199
+ fd: str | PathLike[str] | TextIO,
192
200
  encoding: str = "utf-8",
193
201
  handler: BaseHandler | None = None,
194
202
  **kwargs: object,
@@ -199,11 +207,11 @@ def dump(
199
207
 
200
208
  ::
201
209
 
202
- >>> from io import BytesIO
210
+ >>> from io import StringIO
203
211
  >>> post = frontmatter.load('tests/yaml/hello-world.txt')
204
- >>> f = BytesIO()
212
+ >>> f = StringIO()
205
213
  >>> frontmatter.dump(post, f)
206
- >>> print(f.getvalue().decode('utf-8'))
214
+ >>> print(f.getvalue())
207
215
  ---
208
216
  layout: post
209
217
  title: Hello, world!
@@ -214,11 +222,11 @@ def dump(
214
222
 
215
223
  .. testcode::
216
224
 
217
- from io import BytesIO
225
+ from io import StringIO
218
226
  post = frontmatter.load('tests/yaml/hello-world.txt')
219
- f = BytesIO()
227
+ f = StringIO()
220
228
  frontmatter.dump(post, f)
221
- print(f.getvalue().decode('utf-8'))
229
+ print(f.getvalue())
222
230
 
223
231
  .. testoutput::
224
232
 
@@ -231,13 +239,16 @@ def dump(
231
239
 
232
240
  """
233
241
  content = dumps(post, handler, **kwargs)
234
- if hasattr(fd, "write"):
235
- fd.write(content.encode(encoding))
242
+ if is_writable(fd):
243
+ fd.write(content)
236
244
 
237
- else:
238
- with codecs.open(fd, "w", encoding) as f:
245
+ elif can_open(fd):
246
+ with open(fd, "w", encoding=encoding) as f:
239
247
  f.write(content)
240
248
 
249
+ else:
250
+ raise ValueError(f"Cannot open filename using type {type(fd)}")
251
+
241
252
 
242
253
  def dumps(post: Post, handler: BaseHandler | None = None, **kwargs: object) -> str:
243
254
  """
@@ -278,6 +289,7 @@ def dumps(post: Post, handler: BaseHandler | None = None, **kwargs: object) -> s
278
289
  if handler is None:
279
290
  handler = getattr(post, "handler", None) or YAMLHandler()
280
291
 
292
+ assert handler is not None
281
293
  return handler.format(post, **kwargs)
282
294
 
283
295
 
@@ -8,8 +8,8 @@ By default, ``frontmatter`` reads and writes YAML metadata. But maybe
8
8
  you don't like YAML. Maybe enjoy writing metadata in JSON, or TOML, or
9
9
  some other exotic markup not yet invented. For this, there are handlers.
10
10
 
11
- This module includes handlers for YAML, JSON and TOML, as well as a
12
- :py:class:`BaseHandler <frontmatter.default_handlers.BaseHandler>` that
11
+ This module includes handlers for YAML, JSON and TOML, as well as a
12
+ :py:class:`BaseHandler <frontmatter.default_handlers.BaseHandler>` that
13
13
  outlines the basic API and can be subclassed to deal with new formats.
14
14
 
15
15
  **Note**: The TOML handler is only available if the `toml <https://pypi.org/project/toml/>`_
@@ -32,10 +32,10 @@ A handler needs to do four things:
32
32
 
33
33
  An example:
34
34
 
35
- Calling :py:func:`frontmatter.load <frontmatter.load>` (or :py:func:`loads <frontmatter.loads>`)
36
- with the ``handler`` argument tells frontmatter which handler to use.
37
- The handler instance gets saved as an attribute on the returned post
38
- object. By default, calling :py:func:`frontmatter.dumps <frontmatter.dumps>`
35
+ Calling :py:func:`frontmatter.load <frontmatter.load>` (or :py:func:`loads <frontmatter.loads>`)
36
+ with the ``handler`` argument tells frontmatter which handler to use.
37
+ The handler instance gets saved as an attribute on the returned post
38
+ object. By default, calling :py:func:`frontmatter.dumps <frontmatter.dumps>`
39
39
  on the post will use the attached handler.
40
40
 
41
41
 
@@ -67,7 +67,7 @@ on the post will use the attached handler.
67
67
  <BLANKLINE>
68
68
  And this shouldn't break.
69
69
 
70
- Passing a new handler to :py:func:`frontmatter.dumps <frontmatter.dumps>`
70
+ Passing a new handler to :py:func:`frontmatter.dumps <frontmatter.dumps>`
71
71
  (or :py:func:`dump <frontmatter.dump>`) changes the export format:
72
72
 
73
73
  ::
@@ -283,6 +283,7 @@ class JSONHandler(BaseHandler):
283
283
  END_DELIMITER = ""
284
284
 
285
285
  def split(self, text: str) -> tuple[str, str]:
286
+ assert self.FM_BOUNDARY is not None
286
287
  _, fm, content = self.FM_BOUNDARY.split(text, 2)
287
288
  return "{" + fm + "}", content
288
289
 
@@ -298,7 +299,7 @@ class JSONHandler(BaseHandler):
298
299
 
299
300
  if toml:
300
301
 
301
- class TOMLHandler(BaseHandler):
302
+ class TOMLHandler(BaseHandler): # pyright: ignore
302
303
  """
303
304
  Load and export TOML metadata.
304
305
 
@@ -0,0 +1 @@
1
+ # Marker file for PEP 561. This package uses inline types.
@@ -0,0 +1,31 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Utilities for handling unicode and other repetitive bits
4
+ """
5
+ from os import PathLike
6
+ from typing import TypeGuard, TextIO
7
+
8
+
9
+ def is_readable(fd: object) -> TypeGuard[TextIO]:
10
+ return callable(getattr(fd, "read", None))
11
+
12
+
13
+ def is_writable(fd: object) -> TypeGuard[TextIO]:
14
+ return callable(getattr(fd, "write", None))
15
+
16
+
17
+ def can_open(fd: object) -> TypeGuard[str | PathLike[str]]:
18
+ return isinstance(fd, str) or isinstance(fd, PathLike)
19
+
20
+
21
+ def u(text: str | bytes, encoding: str = "utf-8") -> str:
22
+ "Return unicode text, no matter what"
23
+
24
+ if isinstance(text, bytes):
25
+ text_str: str = text.decode(encoding)
26
+ else:
27
+ text_str = str(text)
28
+
29
+ # it's already unicode
30
+ text_str = text_str.replace("\r\n", "\n")
31
+ return text_str
@@ -0,0 +1,50 @@
1
+ [project]
2
+ name = "python-frontmatter"
3
+ version = "1.3.0"
4
+ description = "Parse and manage posts with YAML (or other) frontmatter"
5
+ readme = "README.md"
6
+ authors = [{ name = "Chris Amico", email = "eyeseast@gmail.com" }]
7
+ requires-python = ">=3.10"
8
+ dependencies = ["PyYAML"]
9
+ license = "MIT"
10
+ license-files = ["LICENSE"]
11
+ keywords = ["frontmatter"]
12
+ classifiers = [
13
+ "Development Status :: 5 - Production/Stable",
14
+ "Intended Audience :: Developers",
15
+ "Natural Language :: English",
16
+ "Programming Language :: Python :: 3",
17
+ "Programming Language :: Python :: 3.10",
18
+ "Programming Language :: Python :: 3.11",
19
+ "Programming Language :: Python :: 3.12",
20
+ "Programming Language :: Python :: 3.13",
21
+ ]
22
+ [project.urls]
23
+ repository = "https://github.com/eyeseast/python-frontmatter"
24
+ issues = "https://github.com/eyeseast/python-frontmatter/issues"
25
+ documentation = "http://python-frontmatter.rtfd.io"
26
+
27
+ [project.optional-dependencies]
28
+ test = ["pytest", "toml", "pyaml", "mypy", "types-PyYAML", "types-toml"]
29
+ docs = ["sphinx"]
30
+ toml = ["toml"]
31
+
32
+ [dependency-groups]
33
+ dev = [
34
+ "pytest",
35
+ "toml",
36
+ "pyaml",
37
+ "mypy",
38
+ "types-PyYAML",
39
+ "types-toml",
40
+ "sphinx",
41
+ ]
42
+
43
+
44
+ [build-system]
45
+ requires = ["uv_build>=0.11.15,<0.12"]
46
+ build-backend = "uv_build"
47
+
48
+ [tool.uv.build-backend]
49
+ module-root = ""
50
+ module-name = "frontmatter"
@@ -1,6 +0,0 @@
1
- include LICENSE
2
- include README.md
3
-
4
- recursive-include tests *
5
-
6
- exclude *.py[co]
@@ -1,150 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: python-frontmatter
3
- Version: 1.1.0
4
- Summary: Parse and manage posts with YAML (or other) frontmatter
5
- Home-page: https://github.com/eyeseast/python-frontmatter
6
- Author: Chris Amico
7
- Author-email: eyeseast@gmail.com
8
- License: MIT
9
- Keywords: frontmatter
10
- Classifier: Development Status :: 5 - Production/Stable
11
- Classifier: Intended Audience :: Developers
12
- Classifier: License :: OSI Approved :: MIT License
13
- Classifier: Natural Language :: English
14
- Classifier: Programming Language :: Python :: 3
15
- Classifier: Programming Language :: Python :: 3.8
16
- Classifier: Programming Language :: Python :: 3.9
17
- Classifier: Programming Language :: Python :: 3.10
18
- Classifier: Programming Language :: Python :: 3.11
19
- Classifier: Programming Language :: Python :: 3.12
20
- Description-Content-Type: text/markdown
21
- Provides-Extra: test
22
- Provides-Extra: docs
23
- License-File: LICENSE
24
-
25
- # Python Frontmatter
26
-
27
- [Jekyll](http://jekyllrb.com/)-style YAML front matter offers a useful way to add arbitrary, structured metadata to text documents, regardless of type.
28
-
29
- This is a small package to load and parse files (or just text) with YAML (or JSON, TOML or other) front matter.
30
-
31
- [![Tests](https://github.com/eyeseast/python-frontmatter/workflows/Test/badge.svg)](https://github.com/eyeseast/python-frontmatter/actions?query=workflow%3ATest)
32
- [![PyPI](https://img.shields.io/pypi/v/python-frontmatter.svg)](https://pypi.org/project/python-frontmatter/)
33
-
34
- **[Documentation](https://python-frontmatter.readthedocs.io/en/latest/)**
35
-
36
- ## Install:
37
-
38
- pip install python-frontmatter
39
-
40
- ## Usage:
41
-
42
- ```python
43
- >>> import frontmatter
44
-
45
- ```
46
-
47
- Load a post from a filename:
48
-
49
- ```python
50
- >>> post = frontmatter.load('tests/yaml/hello-world.txt')
51
-
52
- ```
53
-
54
- Or a file (or file-like object):
55
-
56
- ```python
57
- >>> with open('tests/yaml/hello-world.txt') as f:
58
- ... post = frontmatter.load(f)
59
-
60
- ```
61
-
62
- Or load from text:
63
-
64
- ```python
65
- >>> with open('tests/yaml/hello-world.txt') as f:
66
- ... post = frontmatter.loads(f.read())
67
-
68
- ```
69
-
70
- If the file has a [Byte-Order Mark](https://en.wikipedia.org/wiki/Byte_order_mark) (BOM), strip it off first. An easy way to do this is by using the [`utf-8-sig`](https://docs.python.org/3/library/codecs.html?highlight=utf%208%20sig#module-encodings.utf_8_sig) encoding:
71
-
72
- ```python
73
- >>> with open('tests/yaml/hello-world.txt', encoding="utf-8-sig") as f:
74
- ... post = frontmatter.load(f)
75
-
76
- ```
77
-
78
- Access content:
79
-
80
- ```python
81
- >>> print(post.content)
82
- Well, hello there, world.
83
-
84
- # this works, too
85
- >>> print(post)
86
- Well, hello there, world.
87
-
88
- ```
89
-
90
- Use metadata (metadata gets proxied as post keys):
91
-
92
- ```python
93
- >>> print(post['title'])
94
- Hello, world!
95
-
96
- ```
97
-
98
- Metadata is a dictionary, with some handy proxies:
99
-
100
- ```python
101
- >>> sorted(post.keys())
102
- ['layout', 'title']
103
-
104
- >>> from pprint import pprint
105
- >>> post['excerpt'] = 'tl;dr'
106
- >>> pprint(post.metadata)
107
- {'excerpt': 'tl;dr', 'layout': 'post', 'title': 'Hello, world!'}
108
-
109
- ```
110
-
111
- If you don't need the whole post object, just parse:
112
-
113
- ```python
114
- >>> with open('tests/yaml/hello-world.txt') as f:
115
- ... metadata, content = frontmatter.parse(f.read())
116
- >>> print(metadata['title'])
117
- Hello, world!
118
-
119
- ```
120
-
121
- Write back to plain text, too:
122
-
123
- ```python
124
- >>> print(frontmatter.dumps(post)) # doctest: +NORMALIZE_WHITESPACE
125
- ---
126
- excerpt: tl;dr
127
- layout: post
128
- title: Hello, world!
129
- ---
130
- Well, hello there, world.
131
-
132
- ```
133
-
134
- Or write to a file (or file-like object):
135
-
136
- ```python
137
- >>> from io import BytesIO
138
- >>> f = BytesIO()
139
- >>> frontmatter.dump(post, f)
140
- >>> print(f.getvalue().decode('utf-8')) # doctest: +NORMALIZE_WHITESPACE
141
- ---
142
- excerpt: tl;dr
143
- layout: post
144
- title: Hello, world!
145
- ---
146
- Well, hello there, world.
147
-
148
- ```
149
-
150
- For more examples, see files in the `tests/` directory. Each sample file has a corresponding `.result.json` file showing the expected parsed output. See also the `examples/` directory, which covers more ways to customize input and output.
@@ -1,18 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
- Utilities for handling unicode and other repetitive bits
4
- """
5
- from typing import AnyStr
6
-
7
-
8
- def u(text: AnyStr, encoding: str = "utf-8") -> str:
9
- "Return unicode text, no matter what"
10
-
11
- if isinstance(text, bytes):
12
- text_str: str = text.decode(encoding)
13
- else:
14
- text_str = text
15
-
16
- # it's already unicode
17
- text_str = text_str.replace("\r\n", "\n")
18
- return text_str
@@ -1,40 +0,0 @@
1
- LICENSE
2
- MANIFEST.in
3
- README.md
4
- setup.py
5
- frontmatter/__init__.py
6
- frontmatter/conftest.py
7
- frontmatter/default_handlers.py
8
- frontmatter/util.py
9
- python_frontmatter.egg-info/PKG-INFO
10
- python_frontmatter.egg-info/SOURCES.txt
11
- python_frontmatter.egg-info/dependency_links.txt
12
- python_frontmatter.egg-info/not-zip-safe
13
- python_frontmatter.egg-info/requires.txt
14
- python_frontmatter.egg-info/top_level.txt
15
- tests/stub_tests.py
16
- tests/test_docs.py
17
- tests/test_files.py
18
- tests/unit_test.py
19
- tests/empty/empty-frontmatter.result.json
20
- tests/empty/empty-frontmatter.txt
21
- tests/empty/no-frontmatter.result.json
22
- tests/empty/no-frontmatter.txt
23
- tests/json/hello-json.md
24
- tests/json/hello-json.result.json
25
- tests/toml/hello-toml.md
26
- tests/toml/hello-toml.result.json
27
- tests/yaml/chinese.result.json
28
- tests/yaml/chinese.txt
29
- tests/yaml/extra-dash.result.json
30
- tests/yaml/extra-dash.txt
31
- tests/yaml/extra-space.result.json
32
- tests/yaml/extra-space.txt
33
- tests/yaml/hello-markdown.md
34
- tests/yaml/hello-markdown.result.json
35
- tests/yaml/hello-world.result.json
36
- tests/yaml/hello-world.txt
37
- tests/yaml/network-diagrams.md
38
- tests/yaml/network-diagrams.result.json
39
- tests/yaml/unpretty.md
40
- tests/yaml/unpretty.result.json
@@ -1,12 +0,0 @@
1
- PyYAML
2
-
3
- [docs]
4
- sphinx
5
-
6
- [test]
7
- pytest
8
- toml
9
- pyaml
10
- mypy
11
- types-PyYAML
12
- types-toml
@@ -1,4 +0,0 @@
1
- [egg_info]
2
- tag_build =
3
- tag_date = 0
4
-