Skip to content

Commit e201492

Browse files
authored
Merge pull request #1078 from fastai/alias-bug
improve jekyll migration
2 parents 6515a92 + bb4627a commit e201492

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

nbdev/migrate.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,19 @@ def _is_jekyll_post(path): return bool(re.search(r'^\d{4}-\d{2}-\d{2}-', Path(pa
6161

6262
def _fp_convert(fm:dict, path:Path):
6363
"Make fastpages frontmatter Quarto complaint and add redirects."
64+
fs = _file_slug(path)
6465
if _is_jekyll_post(path):
6566
fm = compose(_fp_fm, _fp_image)(fm)
6667
if 'permalink' in fm: fm['aliases'] = [f"{fm['permalink'].strip()}"]
67-
else: fm['aliases'] = [f'{_cat_slug(fm) + _file_slug(path)}']
68+
else: fm['aliases'] = [f'{_cat_slug(fm) + fs}']
69+
if not fm.get('date'):
70+
_,y,m,d,_ = fs.split('/')
71+
fm['date'] = f'{y}-{m}-{d}'
6872

73+
if fm.get('summary') and not fm.get('description'): fm['description'] = fm['summary']
74+
if fm.get('tags') and not fm.get('categories'):
75+
if isinstance(fm['tags'], str): fm['categories'] = fm['tags'].split()
76+
elif isinstance(fm['tags'], list): fm['categories'] = fm['tags']
6977
for k in ['title', 'description']:
7078
if k in fm: fm[k] = _rm_quote(fm[k])
7179
if fm.get('comments'): fm.pop('comments') #true by itself is not a valid value for comments https://quarto.org/docs/output-formats/html-basics.html#commenting, and the default is true

nbs/api/migrate.ipynb

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,19 @@
183183
"\n",
184184
"def _fp_convert(fm:dict, path:Path):\n",
185185
" \"Make fastpages frontmatter Quarto complaint and add redirects.\"\n",
186+
" fs = _file_slug(path)\n",
186187
" if _is_jekyll_post(path):\n",
187188
" fm = compose(_fp_fm, _fp_image)(fm)\n",
188189
" if 'permalink' in fm: fm['aliases'] = [f\"{fm['permalink'].strip()}\"]\n",
189-
" else: fm['aliases'] = [f'{_cat_slug(fm) + _file_slug(path)}']\n",
190+
" else: fm['aliases'] = [f'{_cat_slug(fm) + fs}']\n",
191+
" if not fm.get('date'): \n",
192+
" _,y,m,d,_ = fs.split('/')\n",
193+
" fm['date'] = f'{y}-{m}-{d}'\n",
190194
" \n",
195+
" if fm.get('summary') and not fm.get('description'): fm['description'] = fm['summary']\n",
196+
" if fm.get('tags') and not fm.get('categories'): \n",
197+
" if isinstance(fm['tags'], str): fm['categories'] = fm['tags'].split()\n",
198+
" elif isinstance(fm['tags'], list): fm['categories'] = fm['tags']\n",
191199
" for k in ['title', 'description']:\n",
192200
" if k in fm: fm[k] = _rm_quote(fm[k])\n",
193201
" if fm.get('comments'): fm.pop('comments') #true by itself is not a valid value for comments https://quarto.org/docs/output-formats/html-basics.html#commenting, and the default is true\n",
@@ -302,6 +310,7 @@
302310
"categories:\n",
303311
"- fastcore\n",
304312
"- fastai\n",
313+
"date: '2020-09-01'\n",
305314
"description: A unique python library that extends the python programming language\n",
306315
" and provides utilities that enhance productivity.\n",
307316
"draft: 'true'\n",
@@ -341,6 +350,7 @@
341350
"categories:\n",
342351
"- fastcore\n",
343352
"- fastai\n",
353+
"date: '2020-09-01'\n",
344354
"description: A unique python library that extends the python programming language\n",
345355
" and provides utilities that enhance productivity.\n",
346356
"draft: 'true'\n",
@@ -362,6 +372,7 @@
362372
"badges: true\n",
363373
"categories:\n",
364374
"- jupyter\n",
375+
"date: '2020-02-20'\n",
365376
"description: A tutorial of fastpages for Jupyter notebooks.\n",
366377
"image: images/chart-preview.png\n",
367378
"output-file: 2020-02-20-test.html\n",
@@ -466,6 +477,7 @@
466477
"- /markdown/2020/01/14/test-markdown-post\n",
467478
"categories:\n",
468479
"- markdown\n",
480+
"date: '2020-01-14'\n",
469481
"description: A minimal example of using markdown with fastpages.\n",
470482
"layout: post\n",
471483
"title: An Example Markdown Post\n",
@@ -477,7 +489,7 @@
477489
"\n",
478490
"## Basic setup\n",
479491
"\n",
480-
"Jekyll requires blog post files to be named according t\n"
492+
"Jekyll requires blog post files to b\n"
481493
]
482494
}
483495
],
@@ -499,6 +511,7 @@
499511
"- /markdown/2020/01/14/test-markdown-post\n",
500512
"categories:\n",
501513
"- markdown\n",
514+
"date: '2020-01-14'\n",
502515
"description: A minimal example of using markdown with fastpages.\n",
503516
"layout: post\n",
504517
"title: An Example Markdown Post\n",
@@ -518,7 +531,27 @@
518531
"source": [
519532
"#hide\n",
520533
"_res = fp_md_fm('../../tests/2022-09-06-homeschooling.md')\n",
521-
"assert \"aliases:\\n- /2022/09/06/homeschooling\" in _res"
534+
"test_eq(_res,\n",
535+
"\"\"\"---\n",
536+
"aliases:\n",
537+
"- /2022/09/06/homeschooling\n",
538+
"author: Rachel Thomas\n",
539+
"categories:\n",
540+
"- advice\n",
541+
"- health\n",
542+
"date: '2022-09-06'\n",
543+
"description: You can permanently damage your back, neck, and wrists from working without\n",
544+
" an ergonomic setup. Learn how to create one for less at home.\n",
545+
"image: /images/ergonomic1-short.jpg\n",
546+
"summary: You can permanently damage your back, neck, and wrists from working without\n",
547+
" an ergonomic setup. Learn how to create one for less at home.\n",
548+
"tags: advice health\n",
549+
"title: 'Essential Work-From-Home Advice: Cheap and Easy Ergonomic Setups'\n",
550+
"\n",
551+
"---\n",
552+
"\n",
553+
"Lorem ipsum\n",
554+
"\"\"\")"
522555
]
523556
},
524557
{
@@ -636,7 +669,7 @@
636669
"text/markdown": [
637670
"---\n",
638671
"\n",
639-
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/migrate.py#L111){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
672+
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/migrate.py#L118){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
640673
"\n",
641674
"### _repl_v1dir\n",
642675
"\n",
@@ -647,7 +680,7 @@
647680
"text/plain": [
648681
"---\n",
649682
"\n",
650-
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/migrate.py#L111){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
683+
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/migrate.py#L118){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
651684
"\n",
652685
"### _repl_v1dir\n",
653686
"\n",
@@ -745,7 +778,7 @@
745778
"text/markdown": [
746779
"---\n",
747780
"\n",
748-
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/migrate.py#L123){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
781+
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/migrate.py#L130){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
749782
"\n",
750783
"### _convert_callout\n",
751784
"\n",
@@ -756,7 +789,7 @@
756789
"text/plain": [
757790
"---\n",
758791
"\n",
759-
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/migrate.py#L123){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
792+
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/migrate.py#L130){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
760793
"\n",
761794
"### _convert_callout\n",
762795
"\n",
@@ -889,7 +922,7 @@
889922
"text/markdown": [
890923
"---\n",
891924
"\n",
892-
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/migrate.py#L130){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
925+
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/migrate.py#L137){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
893926
"\n",
894927
"### _convert_video\n",
895928
"\n",
@@ -900,7 +933,7 @@
900933
"text/plain": [
901934
"---\n",
902935
"\n",
903-
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/migrate.py#L130){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
936+
"[source](https://github.com/fastai/nbdev/blob/master/nbdev/migrate.py#L137){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
904937
"\n",
905938
"### _convert_video\n",
906939
"\n",
@@ -1065,6 +1098,7 @@
10651098
"- /markdown/2020/01/14/test-markdown-post-COPY\n",
10661099
"categories:\n",
10671100
"- markdown\n",
1101+
"date: '2020-01-14'\n",
10681102
"description: A minimal example of using markdown with fastpages.\n",
10691103
"layout: post\n",
10701104
"title: An Example Markdown Post\n",

tests/2022-09-06-homeschooling.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
---
2-
title: "My family's unlikely homeschooling journey"
3-
date: 2022-09-06
2+
title: "Essential Work-From-Home Advice: Cheap and Easy Ergonomic Setups"
3+
summary: "You can permanently damage your back, neck, and wrists from working without an ergonomic setup. Learn how to create one for less at home."
4+
image: '/images/ergonomic1-short.jpg'
5+
author: 'Rachel Thomas'
6+
tags: advice health
47
---
58

69
Lorem ipsum

0 commit comments

Comments
 (0)