从hexo迁移到hugo

hexo2hugo.py

这里有个脚本:https://github.com/wd/hexo2hugo ,但是有些问题,使用的是 python3 ,一定要注意。 categories 如果是列表会出错, 添加转换

1
2
3
4
5
if 'tags' in meta and type(meta['tags']) == str:
    meta['tags'] = meta['tags'].split(',')

if 'categories' in meta and type(meta['categories']) == str:
    meta['categories'] = meta['categories'].split(',')

时间不对的时候要出错:

1
2
3
4
5
6
7
no_tz_date = meta['date']
try:
    if type(no_tz_date) == str:
        no_tz_date = datetime.datetime.strptime(no_tz_date, '%Y-%m-%d %H:%M')
except:
    if type(no_tz_date) == str:
        no_tz_date = datetime.datetime.strptime(no_tz_date, '%Y-%m-%d %H:%M:%S')

因为没有 more tag,在主页summary不好看,给加上

1
2
3
more_tag=r"<! --more-->"
if body.find(more_tag) == -1:
    body = body + more_tag

完整代码见这里

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import argparse
import os
import logging
import yaml
import pytoml as toml
import datetime
import re

default_logging_level = logging.WARNING
default_timezome_offset = +datetime.timedelta(hours=8)


class Logger(object):
    def __init__(self, name):
        logformat = 'Hexo2hugo (%(name)s): [%(levelname)s] %(message)s'

        self.logger = logging.getLogger(name or __name__)
        self.logger.setLevel(default_logging_level)
        myhandler = logging.StreamHandler()
        myhandler.setFormatter(logging.Formatter(logformat))
        self.logger.addHandler(myhandler)


class Hexo2Hugo(object):
    def __init__(self, src, dest, remove_date):
        self.root_path = os.path.expanduser(src)
        self.dest_path = os.path.expanduser(dest)
        self.remove_date = remove_date
        self.logger = Logger("Hexo").logger

        self._find_all_files()

    def go(self):
        for post in self._process_files():
            name = post['name']
            meta = post['meta']
            body = post['body']

            if self.remove_date:
                name = self._remove_date(name)

            with open(os.path.join(self.dest_path, name), 'w+') as fp:
                self.logger.info("Write to {}, meta: {}".format(name, meta))
                fp.writelines("+++\n{}+++\n\n{}".format(meta, body))

    def _remove_date(self, name):
        new_name = re.sub('^\d+-\d+-\d+-(.*)', r'\1', name)
        if new_name:
            return new_name
        return name

    def _find_all_files(self):
        self.files = []
        for file in os.listdir(self.root_path):
            if os.path.isfile(os.path.join(self.root_path, file)):
                self.files.append(file)
        self.logger.info("Total {} files found".format(len(self.files)))

    def _extract_date_from_filename(self, filename):
        m = re.match('^(\d+-\d+-\d+)-.*$', filename)
        if m:
            return m.group(1)

    def _process_files(self):
        for hexo_file in self.files:
            date_from_filename = self._extract_date_from_filename(hexo_file)
            with open(os.path.join(self.root_path, hexo_file), 'r') as fp:
                meta_yaml = ''
                body = ''
                is_meta = True
                is_first_line = True
                is_in_pre = False
                for line in fp:
                    if is_first_line:
                        is_first_line = False
                        if line == '---\n':
                            continue

                    if is_meta:
                        if line == '---\n':
                            is_meta = False
                        else:
                            meta_yaml += line
                    else:
                        if re.search(r'.html$', hexo_file):
                            if re.search(r'^<pre', line):
                                is_in_pre = True

                            if re.search(r'^</pre', line):
                                is_in_pre = False

                            if not is_in_pre and not re.match(r'^$', line) and not re.search(r'<br */>$', line):
                                body += line[:-1] + "<br />\n"
                            else:
                                body += line
                        else:
                            body += line
                meta = yaml.load(meta_yaml)

                self.logger.info("Process {} now, meta: {}, body length: {}".format(hexo_file, meta, len(body)))

                if 'date' in meta:
                    no_tz_date = meta['date']
                    try:
                        if type(no_tz_date) == str:
                            no_tz_date = datetime.datetime.strptime(no_tz_date, '%Y-%m-%d %H:%M')

                    except:
                        if type(no_tz_date) == str:
                            no_tz_date = datetime.datetime.strptime(no_tz_date, '%Y-%m-%d %H:%M:%S')


                    meta['date'] = no_tz_date.replace(tzinfo=datetime.timezone(default_timezome_offset)).isoformat('T')
                else:
                    meta['date'] = date_from_filename

                meta['description'] = ''
                if 'permalink' in meta:
                    meta['slug'] = meta['permalink']
                    del meta['permalink']

                if 'layout' in meta:
                    del meta['layout']

                if 'tags' in meta and type(meta['tags']) == str:
                    meta['tags'] = meta['tags'].split(',')

                if 'categories' in meta and type(meta['categories']) == str:
                    meta['categories'] = meta['categories'].split(',')

                more_tag = "<! --more -->"
                if body.find(more_tag.replace(" ", "")) == -1:
                    body = body + more_tag.replace(" ", "")


                meta_toml = toml.dumps(meta)
                yield({'name': hexo_file, 'meta': meta_toml, 'body': body})


def main(args):
    hexo = Hexo2Hugo(args.src, args.dest, args.remove_date_from_name)
    hexo.go()


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--src', help='Hexo posts directory')
    parser.add_argument('--dest', help='Destination directory')
    parser.add_argument('--remove-date-from-name', help='Remove date from file name', action='store_true')
    parser.add_argument('--verbose', help='Output level', action='store_true')

    args = parser.parse_args()

    if args.verbose:
        default_logging_level = logging.DEBUG

    main(args)