Skip to content

Instantly share code, notes, and snippets.

@netwjx
Last active August 29, 2015 14:04
Show Gist options
  • Save netwjx/7669c91937271fc23fcd to your computer and use it in GitHub Desktop.
Save netwjx/7669c91937271fc23fcd to your computer and use it in GitHub Desktop.
fs = require 'fs'
newname = (f, date, time)->
c = fs.readFileSync f,
encoding: 'utf-8'
[skip, title] = c.match /^Title: ([^\r\n]+)$/m
content = c.replace /^Date: .*$/m, ($0)->
"
CDate: #{ date.slice(0, 4) }-
#{ date.slice 4, 6 }-
#{ date.slice 6, 8 }
#{ time.slice 0, 2 }:
#{ time.slice 2, 4 }
\r\n" + $0
content = content.replace /^(Slug: )\d{4}-\d{2}-\d{2}-/m, '$1'
content = content.replace /^Slug: .*$/m, ($0)->
$0.replace /-/g, ' '
title = title.replace /[:\/\\]/g, ' '
{
orig: f
newname: date[2..] + '-' + time + '_' + title + '.md'
content
}
files = for i in fs.readdirSync './' when /.*\.md$/.test(i)
if m = i.match /^(\d{6})-(\d{4})/
newname i, '20' + m[1], m[2]
else if m = i.match /^(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2})/
newname i, m.slice(1,4).join(''), m.slice(4,6).join('')
else
{
orig: i
newname: i
}
for i in files
# console.log ('-' for j in [0..80]).join('')
if i.content
# console.log i.content.slice 0, i.content.search(/(\r\n|\n|\r)\1/)
fs.writeFileSync i.orig, i.content,
encoding: 'utf-8'
if i.orig and i.newname and i.orig isnt i.newname
console.log i.orig + ' -> ' + i.newname
fs.renameSync i.orig, i.newname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment