最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】优化crifan的Gitbook的template:同步README_current到book_current

gitbook crifan 1134浏览 0评论
之前已建立了自己crifan的Gitbook的template:
crifan/gitbook_template: 演示如何使用crifan的gitbook的模板去创建自己的gitbook电子书
现在发现有个细节需要优化:
希望把
books/china_suitable_living_suzhou/README_current.json
中的内容,同步到:
books/china_suitable_living_suzhou/book_current.json
希望把其中的
  • bookName
  • bookDescription
  • gitRepoName
同步到:
  • title
  • description
  • repo
去写python脚本去处理
然后再去合并到makefile中的sync_content中
最后新增代码是:
common/tools/sync_ReadmeCurrent_to_bookCurrent.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Author: Crifan Li
Update: 20200915
Function: Sync README_current.json content to book_current.json
Note: should run this python file from single gitbook foler
      eg: /Users/crifan/dev/dev_root/gitbook/gitbook_src_root/books/gitbook_demo
"""


import os
import json
import codecs
from pprint import pprint


################################################################################
# Global Config
################################################################################


ReadmeCurrentJsonFilename = "README_current.json"
BookCurrentJsonFilename = "book_current.json"


################################################################################
# Internal Function
################################################################################


def loadJsonFromFile(fullFilename, fileEncoding="utf-8"):
    """load and parse json dict from file"""
    with codecs.open(fullFilename, 'r', encoding=fileEncoding) as jsonFp:
        jsonDict = json.load(jsonFp)
        # logging.debug("Complete load json from %s", fullFilename)
        return jsonDict


def saveJsonToFile(jsonDict, fullFilename, indent=2, fileEncoding="utf-8"):
    """
      save dict json into file
      for non-ascii string, output encoded string, without \\uxxxx
    """
    with codecs.open(fullFilename, 'w', encoding="utf-8") as outputFp:
        json.dump(jsonDict, outputFp, indent=indent, ensure_ascii=False)


################################################################################
# Main Part
################################################################################


readmeCurrentJson = {}
bookCurrentJson = {}


# run python in :
currPath = os.getcwd()
# print("currPath=%s" % currPath)
curDirname = os.path.dirname(currPath)
# print("curDirname=%s" % curDirname) # /Users/crifan/dev/dev_root/gitbook/gitbook_src_root/books
curBasename = os.path.basename(currPath)
# print("curBasename=%s" % curBasename) # gitbook_demo
GitbookSrcRootBooks = curDirname
# print("GitbookSrcRootBooks=%s" % GitbookSrcRootBooks)
GitbookSrcRoot = os.path.abspath(os.path.join(GitbookSrcRootBooks, ".."))
# print("GitbookSrcRoot=%s" % GitbookSrcRoot)


CurrentBookPath = currPath
# print("CurrentBookPath=%s" % CurrentBookPath)


CurrentGitbookName = curBasename
# print("CurrentGitbookName=%s" % CurrentGitbookName)
# youdao_note_summary
# gitbook_demo


readmeCurrentJsonFullPath = os.path.join(CurrentBookPath, ReadmeCurrentJsonFilename)
# print("readmeCurrentJsonFullPath=%s" % readmeCurrentJsonFullPath)
readmeCurrentJson = loadJsonFromFile(readmeCurrentJsonFullPath)


bookCurrentJsonFullPath = os.path.join(CurrentBookPath, BookCurrentJsonFilename)
# print("bookCurrentJsonFullPath=%s" % bookCurrentJsonFullPath)
bookCurrentJson = loadJsonFromFile(bookCurrentJsonFullPath)


# pprint("/"*80)
# pprint(readmeCurrentJson)
# pprint("/"*80)
# pprint(bookCurrentJson)


gitRepoName = readmeCurrentJson["gitRepoName"]
# print("gitRepoName=%s" % gitRepoName)
bookName = readmeCurrentJson["bookName"]
# print("bookName=%s" % bookName)
bookDescription = readmeCurrentJson["bookDescription"]
# print("bookDescription=%s" % bookDescription)


bookCurrentJson["title"] = bookName
bookCurrentJson["description"] = bookDescription
pluginsConfig = bookCurrentJson["pluginsConfig"]
# print("pluginsConfig=%s" % pluginsConfig)
pluginsConfig["github-buttons"]["buttons"][0]["repo"] = gitRepoName


PrefixTemplate = "https://book.crifan.com/gitbook/%s/website/"
newPrefix = PrefixTemplate % gitRepoName
# print("newPrefix=%s" % newPrefix)
pluginsConfig["sitemap-general"]["prefix"] = newPrefix
UrlTemplate = "http://book.crifan.com/books/%s/pdf/%s.pdf"
newUrl = UrlTemplate % (gitRepoName, gitRepoName)
# print("newUrl=%s" % newUrl)
pluginsConfig["toolbar-button"]["url"] = newUrl

# print("Updated %s:" % BookCurrentJsonFilename)
# pprint(bookCurrentJson)
saveJsonToFile(bookCurrentJson, bookCurrentJsonFullPath)
实现要的效果了。
【总结】
最后
(1)代码详见:
https://github.com/crifan/gitbook_template
中的
https://github.com/crifan/gitbook_template/blob/master/common/tools/sync_ReadmeCurrent_to_bookCurrent.py
(2)makefile:
https://github.com/crifan/gitbook_template/blob/master/common/gitbook_makefile.mk
中的调用是:
SYNC_README_JSON_TO_BOOK_JSON=$(GITBOOK_ROOT_COMMON)/tools/sync_ReadmeCurrent_to_bookCurrent.py

## Sync README_current.json to book_current.json
sync_readme_to_book:
    @python $(SYNC_README_JSON_TO_BOOK_JSON)

## sync content
sync_content: sync_readme_to_book generate_book_json generate_readme_md copy_readme copy_gitignore
    @echo Complete sync content
供参考。

转载请注明:在路上 » 【已解决】优化crifan的Gitbook的template:同步README_current到book_current

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.211 seconds, using 22.17MB memory