Skip to content

Instantly share code, notes, and snippets.

@ryo-murai
Created January 24, 2019 09:47
Show Gist options
  • Save ryo-murai/341c1e3f8dd11fe5154018467b0f3fab to your computer and use it in GitHub Desktop.
Save ryo-murai/341c1e3f8dd11fe5154018467b0f3fab to your computer and use it in GitHub Desktop.
GitBucketのissueを静的閲覧用HTMLにする
#!/bin/sh
repo_url=$1
last_issue_num=$2
issues_url="${repo_url}/issues"
gb_host=`echo "$repo_url" | sed 's/^\(http:\/\/[^\/]\+\)\/.*$/\1/'`
gb_host_esc=$(echo "${gb_host}" | sed 's/\//\\\//g')
function print_args () {
echo "* * * * * * : : parameters : : * * * * "
echo "gitbucket repo:${repo_url}"
echo "host: $gb_host"
echo "escaped host: $gb_host_esc"
echo "last issue number: $last_issue_num"
echo "* * * * * * : : * * * * * : : * * * * "
}
function show_usage() {
echo "USAGE: $0 <gitbucket repository url> <latest issue number>"
}
function check_url () {
## check 2nd arg
curl -f -I "${issues_url}/${last_issue_num}"
if [ $? -ne 0 ]; then
echo "ERROR: ${issues_url}/${last_issue_num} not accessible"
echo "ERROR: please check the parameters"
show_usage
exit 4
fi
}
function get_assets () {
# 前準備
# 任意のissueページを落とす
curl "${issues_url}/1" -o issue_1.html
# assets配下のjs,css,画像などをダウンロード
egrep '(href|src)="/assets/' issue_1.html | sed "s/^.*\"\(\/assets[^?]*\)?.*$/curl ${gb_host_esc}\1 --create-dirs -o .\1/g" | sh
# ヘッダ、サイドバーを隠す
echo '.main-header {display: none}' >> assets/common/css/gitbucket.css
echo '.main-sidebar {display: none}' >> assets/common/css/gitbucket.css
}
function get_webfonts () {
font_def_css="octicons.css"
font_css_path=$(find ./assets -type f -name ${font_def_css})
echo "css path: ${font_css_path}"
font_localdir_base=$(echo "${font_css_path}" | sed "s/${font_def_css}//")
echo "font_localdir_base: ${font_localdir_base}"
font_res_urlbase=$(echo "${font_localdir_base}" | sed "s/^\./${gb_host_esc}/")
echo "font_res_urlbase: ${font_res_urlbase}"
font_files=$(grep 'url(' ${font_css_path} | sed 's/^.*url("\([^"]\+\)".*$/\1/g')
for f in ${font_files};
do
file_name=$(echo $f | sed 's/\?.*$//')
curl ${font_res_urlbase}$f --create-dirs -o ${font_localdir_base}${file_name}
done
}
function get_avatar_icons () {
echo "download avatar icons ..."
avatars=$(grep -h -E '<img src="\/.*_avatar\?' issue_*.html | sed 's/<img src=\"\/\(.*\)\/_avatar.*$/\1/' | sort -u)
for avatar in ${avatars}; do curl "${gb_host}/${avatar}/_avatar" --create-dirs -o ./assets/${avatar}/_avatar; done
for f in issue_*.html; do sed 's/<img src="\(\/.*\)\/_avatar\?[^"]\+"/<img src=".\/assets\1\/_avatar"/g' -i $f; done
echo "done download avatar icons and rewrote img urls"
}
function get_issuepages () {
# issueをHTMLでダウンロード
echo "downloading issue pages ..."
for i in $(seq 2 ${last_issue_num}); do curl "${issues_url}/$i" -o issue_$i.html; done
echo "finished downloading issue pages."
# imgタグ2つ以上同じ行に書かれているページがいるので、改行を入れる
for f in *.html; do sed 's/<img/\n<img/g' -i $f; done
# 次の手順の前提条件確認: <img と srcが同じ行にいることを確認
num_img=$(grep '<img' issue_*.html | wc -l)
num_imgsrc=$(grep '<img' issue_*.html | grep 'src=' | wc -l)
# 同じ行数ヒットすること
if [ $num_img -ne $num_imgsrc ]; then
echo "WARNING: there are <img> tag(s) which has src attribute at next line"
echo "WARNING: those img src url wont be rewritten."
echo "----------------------------------------------"
grep -n '<img' issue_*.html | grep -v 'src='
echo "----------------------------------------------"
fi
echo "downloading attachment files ..."
# 添付イメージ(attached)をダウンロード
grep '<img.*_attached' *.html | sed 's/^.*<img src=\"\([^\"]\+\)\(\/_attached\/[^\"]\+\)".*$/curl \1\2 --create-dirs -o .\2 /g' | sh
rel_issue_url=$(echo "${issues_url}" | sed 's/^http:\/\/[^\/]\+\(.*\)$/\1/' | sed 's/\//\\\//g')
# assetsやattachedなどへのリンクに差し替え
# 長いのでsedスクリプトをファイル化(replace_links.sed)
cat << EOS > replace_links.sed
s/\(\/assets[^?]*\)/.\1/g
s/<img src=\"[^\"]\+\(\/_attached\/[^\"]\+\)\"/<img src=\"\.\1\"/g
s/${rel_issue_url}\/\([0-9]\+\)\([^\"]*\)/issue_\1.html\2/g
EOS
echo "-------------------------------------"
cat replace_links.sed
echo "-------------------------------------"
echo "rewritting url for attachment files ..."
# 置換実行
for f in *.html; do sed -f replace_links.sed -i $f; done
echo "done rewritting url for attachment files"
rm replace_links.sed
}
function make_indexpage () {
table=$(grep '<title' `ls -v issue_*.html` | sed 's/^\(issue_[0-9]\+\)\.html.*<title>\(.*\) - rousaiRezept\/tenkenbatch-apf-works<\/title>/<tr><td>\1<\/td><td><a href=\"\1\.html\">\2<\/a><\/td><\/tr>/')
bootstrap_css_path=$(find ./assets -type f -name 'bootstrap.min.css')
cat << EOS > index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>目次</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<link rel="icon" href="./assets/common/images/gitbucket.png" type="image/vnd.microsoft.icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="${bootstrap_css_path}" rel="stylesheet">
</head>
<body>
<table class="table table-striped">
<thead><tr><th>No</th><th>issue</th></tr></thead>
<tbody>
<!-- ここから生成された部分 -->
${table}
<!-- ここまで生成された部分 -->
</tbody>
</table>
</body>
</html>
EOS
}
print_args
check_url
get_assets
get_webfonts
get_issuepages
get_avatar_icons
make_indexpage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment