| 12 | |
| 13 | |
| 14 | def sitemap(suffix='.md'): |
| 15 | # ensure we are in the ROOT_DIR |
| 16 | os.chdir(ROOT_DIR) |
| 17 | multilang = ['en/', 'zh-hans/', 'zh-tw/'] |
| 18 | pages = [] |
| 19 | raw_bytes = check_output('scripts/gitls.sh') |
| 20 | # ignore last blank string |
| 21 | raw_strs = raw_bytes.decode("utf-8").split('\n')[:-1] |
| 22 | for raw_str in raw_strs: |
| 23 | date, raw_f = raw_str.split(' ') |
| 24 | for lang in multilang: |
| 25 | if raw_f.startswith(lang) and raw_f.endswith(suffix): |
| 26 | if raw_f == lang + 'SUMMARY.md': |
| 27 | continue |
| 28 | p = Path(raw_f) |
| 29 | # rename README with index |
| 30 | if p.name == 'README.md': |
| 31 | p = p.with_name('index.md') |
| 32 | p = p.with_suffix('.html') |
| 33 | fn = p.as_posix().lower() |
| 34 | page = {} |
| 35 | page['lastmod'] = date |
| 36 | page['url'] = fn |
| 37 | pages.append(page) |
| 38 | root_url = 'http://algorithm.yuanbin.me' |
| 39 | templates = os.path.join(BASE_DIR, 'sitemap' + os.sep + 'templates') |
| 40 | env = Environment(loader=FileSystemLoader(templates)) |
| 41 | template = env.get_template('sitemap.xml') |
| 42 | sitemap_xml = template.render(root_url=root_url, pages=pages, freq='daily') |
| 43 | sitemap_fn = os.path.join(ROOT_DIR, 'sitemap.xml') |
| 44 | with open(sitemap_fn, 'w') as sf: |
| 45 | sf.write(sitemap_xml) |
| 46 | sitemap_txt_fn = os.path.join(ROOT_DIR, 'sitemap.txt') |
| 47 | with open(sitemap_txt_fn, 'w') as sf: |
| 48 | urls = [root_url + '/' + page['url'] + '\n' for page in pages] |
| 49 | sf.writelines(urls) |
| 50 | # gitbook do not serve static files under root dir |
| 51 | sitemap_en_fn = os.path.join(ROOT_DIR, 'en' + os.sep + 'sitemap.xml') |
| 52 | copyfile(sitemap_fn, sitemap_en_fn) |
| 53 | |
| 54 | |
| 55 | if __name__ == "__main__": |