Is there any command that can rename the post files which are weird encoded?

Previously I tried the TOML to/from YAML convert command to successfully turn my YAML based front matter md files to TOML. It’s cool.

I imported my blog using Hugo import command from Wordpress. Since most of my wordpress blog entries and Chinese titled, the exported/imported file names looked weird, something like %e4%b8%ba%e4%bb%80%e4%b9%88%e8%bd.md.

Obviously, such types of file name is not good for reading and recognizing because all the post files are stored in the same directory. I was wondering if the hugo command can provide something like renaming function, such as

hugo rename date-title dest-dir target-dir

and all the md files in the posts dir can be renamed in the format as “2018-02-28-title.md”, or something like this? I think variables inside the front matter such as author, date, title and maybe id can be extracted, and since wordpress can export correct non-english titles, such a renaming can help avoid the weird file names.

I know nothing about programming. So the current solution by my side is, I exported from wordpress the xml files, imported into hugo(as hugo can retain the time zone information, and runs faster), and put these weird coded files to under the Hexo post directory. With the help of a plugin called hexo-console-rename, I turned my md files all to correct names(which by default is 2018-02-28-title.md). Then, I copied all these renamed files back to hugo again, and by using hugo convert toTOML.

Well, it’s almost done.

Also, I was wondering if I may suggest another function that can insert variables into the existing md post files? For example, the exported files contain information such as guid, id, url and etc, but the default generated md files don’t have these. If hugo command has the insert function, that would be great to insert maybe author’s name according to some certain tags.

Hugo insert author[author="ang"] if tags="computer game"

Well, I don’t know if the command should look like this.

1 Like

Regarding your insert function suggestion, you should open an issue at the Hugo repo on Github and the Devs will reply whether such a feature is feasible.

Edit
Also for creating new Hugo posts you can use template variables in archetypes.

Thank you, @alexandros, for your proposal. I was just replied that the feature was not that feasible for hugo, so the thread there was closed.

Yes, creating new posts can be done via the archetype approach, but for existing, say dozens of post mds, it could be easier to have the insert function enabled.

I recently tried a redlounge theme which suggests adding something like noauthor = true into the front matter of those mds in order to hidding the author display. It could be a very tough job to add nearly 100 entries by opening one md, inserting, and saving, what a big loop.

I have seen questions like these come up frequently in this forum. That makes me think that the likes of sed, awk and perl are a lost art :slight_smile:

Here’s a quickly cooked up find + sed one liner:

find . -name "*.md" | xargs sed -i.bkp '0,/^+++\s*$/! s/^+++\s*$/noauthor = true\n\0/'   

This is just a quick demo. You can get a robust version of this by converting that 1-liner to a little bash script with checks for stuff like “does the file already have noauthor in front-matter”, etc.

To try this out safely: Copy one or more of your current .md files to a temporary directory, say ~/temp/foo, and then run the above command only after cd -ing to that directory.


Warning

  • Works on all .md files in the current and nested subdirectories.
  • MODIFIES the found files, but also creates a backup of the original files by giving a .bkp suffix.
  • You will lose those backups (they will get overwritten) if you run the same command again.
  • To run this risk-free, run the command without the -i.bkp part.
  • It assumes that you have only 2 lines in each .md file that begin contain just +++. Nothing will happen if a .md file contains 0 or 1 lines with +++.
  • With great power comes great responsibility.

Above converts a file with front-matter:

+++
title = "Foo"
tags = ["abc", "def"]
draft = false
+++

to:

+++
title = "Foo"
tags = ["abc", "def"]
draft = false
noauthor = true
+++
1 Like

It’s how I pay the bills. I love the looks I get when co-workers find out I still write new Perl scripts. Funny how they never take me up on my offer to let them write it in their favorite language instead.

Pro tip: never get into a situation where the terms “Perl script” and “quarterly audit” are in the same sentence…

-j

1 Like

Thank you, @kaushalmodi.

I tried your solution, and then realized your scripts are for non-windows environment.

Anyway, I’ve done that manually.