之前已建立了自己crifan的Gitbook的template:
现在发现有个细节需要优化:
希望把
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
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 | #!/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 newPrefix = PrefixTemplate % gitRepoName # print("newPrefix=%s" % newPrefix) pluginsConfig[ "sitemap-general" ][ "prefix" ] = newPrefix newUrl = UrlTemplate % (gitRepoName, gitRepoName) # print("newUrl=%s" % newUrl) pluginsConfig[ "toolbar-button" ][ "url" ] = newUrl # print("Updated %s:" % BookCurrentJsonFilename) # pprint(bookCurrentJson) saveJsonToFile(bookCurrentJson, bookCurrentJsonFullPath) |
实现要的效果了。
【总结】
最后
(1)代码详见:
中的
(2)makefile:
中的调用是:
1 2 3 4 5 6 7 8 9 | 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