66import importlib
77import json
88import os
9+ import posixpath
910import re
1011import subprocess
1112import sys
@@ -69,6 +70,9 @@ def md_to_html(md_data, path, hrefs=None, global_qualify_list=None, global_defin
6970 'base_path' : '/' .join (paths [:- 1 ]),
7071 'full_path' : path + '.md' ,
7172 'extension' : '.html' ,
73+ 'use_relative_link' : settings .USE_RELATIVE_LINK ,
74+ 'image_repo' : settings .IMAGE_REPO ,
75+ 'use_static_image' : settings .IMAGE_DIR is not None ,
7276 }
7377 extension_configs ['codehilite' ] = {
7478 'noclasses' : False
@@ -510,11 +514,11 @@ def convert_pageinfo(pageinfo, sidebar, sidebar_index, template, hrefs, global_q
510514 sidebar .set_active (pageinfo ['paths' ])
511515
512516 content_header = ContentHeader (pageinfo ['paths' ], sidebar , sidebar_index )
513- convert ( pageinfo [ 'path' ], template , {
517+ context = {
514518 'title' : (
515519 pageinfo ['title' ] if pageinfo ['is_index' ] else
516520 pageinfo ['title' ] + settings .TITLE_SUFFIX ),
517- 'url' : settings .BASE_URL + '/' + pageinfo ['href' ],
521+ 'url' : settings .BASE_URL + pageinfo ['href' ],
518522 'description' : pageinfo ['description' ],
519523 'cachebust' : _CACHEBUST ,
520524 'disable_sidebar' : settings .DISABLE_SIDEBAR ,
@@ -530,7 +534,18 @@ def convert_pageinfo(pageinfo, sidebar, sidebar_index, template, hrefs, global_q
530534 'project_name' : settings .PROJECT_NAME ,
531535 'latest_commit_info' : latest_commit_info ,
532536 'keywords' : settings .META_KEYWORDS ,
533- }, hrefs , global_qualify_list , global_defined_words )
537+ 'relative_base' : '' ,
538+ 'relative_index' : '/'
539+ }
540+ if settings .USE_RELATIVE_LINK :
541+ url_current_dir = posixpath .dirname (context ['url' ])
542+ context ['relative_base' ] = posixpath .relpath (settings .BASE_URL , url_current_dir )
543+ # Note: ローカル (file:///) で閲覧時にディレクトリを開いてしまわないように "/" で終わらず "index.html"も明示する。
544+ context ['relative_index' ] = posixpath .relpath (settings .BASE_URL , url_current_dir ) + '/index.html'
545+ # Note: 以下は <meta /> で埋め込む情報なので敢えて相対パスにはしない。
546+ # context['url'] = posixpath.relpath(context['url'], url_current_dir)
547+ # context['rss'] = posixpath.relpath(context['rss'], url_current_dir)
548+ convert (pageinfo ['path' ], template , context , hrefs , global_qualify_list , global_defined_words )
534549
535550
536551def main ():
@@ -607,6 +622,13 @@ def run(pageinfos):
607622 # 静的ファイルをコピーする
608623 subprocess .call (['cp' , '-v' , '-RL' ] + glob .glob (os .path .join (settings .STATIC_DIR , '*' )) + [settings .OUTPUT_DIR ])
609624
625+ # 画像リポジトリ (image) の画像ファイル (*.png, *.jpg, *.svg) をコピーする
626+ if settings .IMAGE_DIR is not None :
627+ os .system ("cd '%s' && find . -name '*.png' -or -name '*.jpg' -or -name '*.svg' | tee /dev/stderr | cpio -updmv '%s'" % (
628+ settings .IMAGE_DIR ,
629+ os .path .relpath (os .path .join (settings .OUTPUT_DIR , 'static/image' ), settings .IMAGE_DIR )
630+ ))
631+
610632
611633if __name__ == '__main__' :
612634 main ()
0 commit comments