Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfrg committed Jul 16, 2018
1 parent 5a95594 commit 25cbc05
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
48 changes: 41 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ PLUGINS = ['ipynb.markup']
### Option 1: Separate MD metadata file

Place the `.ipynb` file in the content folder and create a new file with the
same name as the ipython notebook with extension `.ipynb-meta`.
For example if you have `my_post.ipynb` create a `my_post.ipynb-meta`.
same name as the ipython notebook with extension `.nbdata`.
For example if you have `my_post.ipynb` create a `my_post.nbdata`.

The `.ipynb-meta` should contain the metadata like a regular Markdown based article:
The `.nbdata` should contain the metadata like a regular Markdown based article:

```
Title:
Expand Down Expand Up @@ -100,16 +100,50 @@ First, enable the "metacell" mode globally in your config
IPYNB_USE_METACELL = True
```

Now, you can put the metadata in the first notebook cell in Markdown mode,
like this:
Now, you can put the metadata in the first notebook cell in Markdown mode, like this:

```markdown
- title: My notebook
- author: John Doe
- date: 2018-05-11
- category: pyhton
- tags: pip
```

### Option 3: metadata field in notebook

Open the `.ipynb` file in a text editor or using the Jupyter Notebook editor under "File"
and look for the `metadata` tag should see.

```
{
"metadata": {
"name": "My notebook",
"kernelspec": ...
"version": ...
... { A_LOT_OF_OTHER_STUFF } ...
},
{ A_LOT_OF_OTHER_STUFF }
```

Edit this the `metadata` tag to have the required markdown fields:

```
{
"metadata": {
"name": "My notebook",
"Title": "Notebook using internal metadata",
"Date": "2100-12-31",
"Category": "Category",
"Tags": "tag1,tag2",
"slug": "with-metadata",
"Author": "Me"
... { A_LOT_OF_OTHER_STUFF } ...
},
{ A_LOT_OF_OTHER_STUFF }
```

## Mode B: Liquid tags

**Requires** to install the pelican [liquid_tags plugin](https://github.com/getpelican/pelican-plugins/tree/master/liquid_tags).
Expand Down Expand Up @@ -141,8 +175,8 @@ Summary:

## Recommend mode?

Personally I like Method A - Option 2 since you only need to add a cell to the notebook and I usually write the whole
article in the notebook.
Personally I like Method A - Option 1 since I write the Notebooks first and then I just add
the metadata file and keeps the notebook clean.

The Liquid tag mode provide more flexibility to combine an existing notebook code or output with extra text on a Markdown.
You can also combine 2 or more notebooks in this mode.
Expand Down
15 changes: 7 additions & 8 deletions markup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,19 @@ def read(self, filepath):
# Files
filedir = os.path.dirname(filepath)
filename = os.path.basename(filepath)
metadata_filename = os.path.splitext(filename)[0] + '.ipynb-meta'
metadata_filename = os.path.splitext(filename)[0] + '.nbdata'
metadata_filepath = os.path.join(filedir, metadata_filename)

# When metadata is in an external file, process the MD file using Pelican MD Reader
md_reader = MarkdownReader(self.settings)

if os.path.exists(metadata_filepath):
# When metadata is in an external file, process the MD file using Pelican MD Reader
md_reader = MarkdownReader(self.settings)
_content, metadata = md_reader.read(metadata_filepath)
else:
# No external .md file: Load metadata from ipython notebook file
with open(filepath) as ipynb_file:
doc = json.load(ipynb_file)
if self.settings.get('IPYNB_USE_METACELL'):
# Option 2: Use metadata on notebook cell
# Option 2: Use metadata on the first notebook cell
metacell = "\n".join(doc['cells'][0]['source'])
# Convert Markdown title and listings to standard metadata items
metacell = re.sub(r'^#+\s+', 'title: ', metacell, flags=re.MULTILINE)
Expand Down Expand Up @@ -95,9 +94,9 @@ def read(self, filepath):
md_filename = filename.split('.')[0] + '.md'
md_filepath = os.path.join(filedir, md_filename)
if not os.path.exists(md_filepath):
raise Exception("Could not find metadata in `.ipynb-meta`, inside `.ipynb` or external `.md` file.")
raise Exception("Could not find metadata in `.nbdata` file or inside `.ipynb`")
else:
raise Exception("Could not find metadata in `.ipynb-meta` or inside `.ipynb` but found `.md` file, "
raise Exception("Could not find metadata in `.nbdata` file or inside `.ipynb` but found `.md` file, "
"assuming that this notebook is for liquid tag usage if true ignore this error")

if 'subcells' in metadata:
Expand All @@ -107,7 +106,7 @@ def read(self, filepath):
preprocessors=self.settings.get('IPYNB_PREPROCESSORS', []),
start=start, end=end,
template=self.settings.get('IPYNB_EXPORT_TEMPLATE')
)
)

# Generate summary: Do it before cleaning CSS
use_meta_summary = self.settings.get('IPYNB_GENERATE_SUMMARY', True)
Expand Down

0 comments on commit 25cbc05

Please sign in to comment.