Created
December 18, 2019 16:37
-
-
Save x-projs/187adabcd23a2b452e078287b6d9fdae to your computer and use it in GitHub Desktop.
mdBook preprocessor for adding contributors (git committer) to each chapter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json; | |
import os; | |
import re; | |
import sys; | |
def processBookItem(src_path, item): | |
if 'Chapter' in item: | |
processChapter(src_path, item['Chapter']) | |
def processChapter(src_path, ch): | |
# Get git committer for this chapter | |
file_path = src_path + "/" + ch['path'] | |
output = os.popen('git log --pretty=short "{}" | git shortlog -sne'.format(file_path)).read() | |
s = """ | |
<div class="committers-div"> | |
<h4>Contributors of this page</h4> | |
""" | |
for logline in output.splitlines(): | |
group = re.search("\t(.+) <([^@]+)@([^>]+)>$", logline) | |
if group: | |
name = group.group(1) | |
alias = group.group(2) | |
company = group.group(3) | |
email = alias + "@" + company | |
if company.lower() == "microsoft.com": | |
s = s + '<a href="https://who/is/{}" title="{}"><img alt="{}" src="http://msrtc/p/avatar.php?{}"></a>\n'.format(alias, name, name, email) | |
else: | |
s = s + '<a href="#" title="{}"><img alt="{}" src="http://msrtc/p/avatar.php?{}"></a>\n'.format(name, name, email) | |
s = s + """ | |
</div> | |
""" | |
ch['content'] = ch['content'] + s | |
for item in ch['sub_items']: | |
processBookItem(src_path, item) | |
def main(): | |
if 'supports' in sys.argv: | |
sys.exit(0) | |
(config, book) = json.loads(sys.stdin.readlines()[0]) | |
src_path = config['config']['book']['src'] | |
sections = book['sections'] | |
for item in sections: | |
processBookItem(src_path, item) | |
print json.dumps(book) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment