用正则表达式重写 YAML frontmatter [英] Rewrite YAML frontmatter with regular expression
问题描述
我想使用 Jekyll 将我的 WordPress 网站转换为 GitHub 上的静态网站.
I want to convert my WordPress website to a static site on GitHub using Jekyll.
我使用了一个插件,可以将我的 62 个帖子作为 Markdown 导出到 GitHub.我现在在每个文件的开头都有这些带有额外前置内容的帖子.它看起来像这样:
I used a plugin that exports my 62 posts to GitHub as Markdown. I now have these posts with extra frontmatter at the beginning of each file. It looks like this:
---
ID: 51
post_title: Here's my post title
author: Frank Meeuwsen
post_date: 2014-07-03 22:10:11
post_excerpt: ""
layout: post
permalink: >
https://myurl.com/slug
published: true
sw_timestamp:
- "399956"
sw_open_thumbnail_url:
- >
https://myurl.com/wp-content/uploads/2014/08/Featured_image.jpg
sw_cache_timestamp:
- "408644"
swp_open_thumbnail_url:
- >
https://myurl.com/wp-content/uploads/2014/08/Featured_image.jpg
swp_open_graph_image_data:
- '["https://i0.wp.com/myurl.com/wp-content/uploads/2014/08/Featured_image.jpg?fit=800%2C400&ssl=1",800,400,false]'
swp_cache_timestamp:
- "410228"
---
这个块没有被 Jekyll 正确解析,而且我不需要所有这些 frontmatter.我想将每个文件的 frontmatter 转换为
This block isn't parsed right by Jekyll, plus I don't need all this frontmatter. I would like to have each file's frontmatter converted to
---
ID: 51
post_title: Here's my post title
author: Frank Meeuwsen
post_date: 2014-07-03 22:10:11
layout: post
published: true
---
我想用正则表达式来做到这一点.但是我对正则表达式的了解并不是那么好.在这个论坛的帮助和大量的谷歌搜索的帮助下,我并没有走多远.我知道如何找到完整的 frontmatter,但如何用上面指定的一部分替换它?
I would like to do this with regular expressions. But my knowledge of regex is not that great. With the help of this forum and lots of Google searches I didn't get very far. I know how to find the complete piece of frontmatter but how do I replace it with a part of it as specified above?
我可能需要分步执行此操作,但我不知道如何执行此操作.
I might have to do this in steps, but I can't wrap my head around how to do this.
我使用 Textwrangler 作为编辑器来进行搜索和替换.
I use Textwrangler as the editor to do the search and replace.
推荐答案
编辑我的帖子,因为我第一次误解了问题,我没能理解实际帖子在同一个文件中,就在 ---
使用 egrep 和 GNU sed,所以不是内置的 bash,相对容易:
Using egrep and GNU sed, so not the bash built-in, it's relatively easy:
# create a working copy
mv file file.old
# get only the fields you need from the frontmatter and redirect that to a new file
egrep '(---|ID|post_title|author|post_date|layout|published)' file.old > file
# get everything from the old file, but discard the frontmatter
cat file.old |gsed '/---/,/---/ d' >> file
# remove working copy
rm file.old
如果您想要一次性完成:
And if you want it all in one go:
for i in `ls`; do mv $i $i.old; egrep '(---|ID|post_title|author|post_date|layout|published)' $i.old > $i; cat $.old |gsed '/---/,/---/ d' >> $i; rm $i.old; done
为了更好的衡量,以下是我写的第一条回复:
For good measure, here's what I wrote as my first response:
============================================================
===========================================================
我认为你把这种方式变得太复杂了.
I think you're making this way too complicated.
一个简单的 egrep 可以满足您的需求:
A simple egrep will do what you want:
egrep '(---|ID|post_title|author|post_date|layout|published)' file
重定向到一个新文件:
egrep '(---|ID|post_title|author|post_date|layout|published)' file > newfile
一次整个目录:
for i in `ls`; do egrep '(---|ID|post_title|author|post_date|layout|published)' $i > $i.new; done
这篇关于用正则表达式重写 YAML frontmatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!