Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
JSON metadata (#2804)
  • Loading branch information
ralsina committed May 28, 2017
1 parent 1ea109b commit 574621f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 1 addition & 2 deletions CHANGES.txt
Expand Up @@ -4,8 +4,7 @@ New in master
Features
--------


* Support for YAML and TOML metadata (Issue #2801)
* Support for JSON, YAML and TOML metadata (Issue #2801)

New in v7.8.6
=============
Expand Down
11 changes: 11 additions & 0 deletions docs/manual.txt
Expand Up @@ -312,6 +312,17 @@ TOML metadata should be wrapped by a "+++" separator and in that case, the usual
date = "2012-09-15 19:52:05 UTC"
+++

JSON metadata is wrapped in braces and the usual JSON syntax applies. Be careful not to leave any
braces alone in a line other than the last one:

.. code:: json

{
title: "How to make money"
slug: "how-to-make-money"
date: "2012-09-15 19:52:05 UTC"
}


Basic
`````
Expand Down
10 changes: 10 additions & 0 deletions nikola/post.py
Expand Up @@ -1053,6 +1053,16 @@ def _get_metadata_from_file(meta_data):
meta = toml.load('\n'.join(meta_data[1:idx]))
return meta

# If 1st line is '{', then it's JSON metadata
if meta_data[0] == '{':
idx = meta_data.index('}', 1)
meta = json.loads('\n'.join(meta_data[1:idx]))
# We expect empty metadata to be '', not None
for k in meta:
if meta[k] is None:
meta[k] = ''
return meta

# First, get metadata from the beginning of the file,
# up to first empty line

Expand Down

0 comments on commit 574621f

Please sign in to comment.