Skip to content

Instantly share code, notes, and snippets.

@bs2kbs2k
Last active December 22, 2020 01:36
Show Gist options
  • Save bs2kbs2k/840faef94e312e72620bbe02c64f33c3 to your computer and use it in GitHub Desktop.
Save bs2kbs2k/840faef94e312e72620bbe02c64f33c3 to your computer and use it in GitHub Desktop.
#!/bin/bash
LIST=$(cat SUMMARY.md|grep -oe "[^(]*md"|sed ':a;N;$!ba;s/\n/ /g'|sed 's/ ^//')
mkdir tmp
for file in $LIST
do
cp $file tmp/
done
mv $(echo $LIST|python3 map.py) tmp/dmp.json
cp -r img tmp/
cd tmp
sed -i "s#\.\./#https://doc.rust-lang.org/#g" $LIST
for file in $(echo $LIST)
do
sed -E -i 's/(('$(echo $LIST|sed 's/ /)|(/g'|sed 's/md/html/g')'))#/#/g' $file
sed -E -i 's/('$(echo $LIST|sed 's/ /)|(/g'|sed 's/md/html/g')')/!!REPLACE!!\0!!REPLACE!!/g' $file
sed -E -i 's/```rust.*/```rust/g' $file
done
python3 ../prep.py
wget https://nostarch.com/sites/default/files/styles/uc_product_full/public/Rust2018_front.png
cat << 'EOF' > metadata.txt
---
title: The Rust Programming Language
author:
- Steve Klabnik
- Carol Nichols
identifier:
- scheme: ISBN-13
- text: 9781718500440
lang: en-US
cover-image: Rust2018_front.png
...
EOF
pandoc $(echo $@) -o TRPL.epub metadata.txt $(echo $LIST)
import json
import time
import re
import traceback
import sys
fnames = input().split(' ')
mydict = {}
for fname in fnames:
try:
f = open(fname.strip(' '),'r')
stripped = f.readline().strip('# \n')
lowered =stripped.lower()
mydict[fname.strip(' ')] = re.sub(r'[^a-z\-]+', '', lowered.replace(' ', '-'))
f.close()
except Exception as e:
print('failed opening',fname,file=sys.stderr)
print(traceback.format_exc(),file=sys.stderr)
t = str(int(time.time()))
f = open('dump'+t+'.json','w')
f.write(json.dumps(mydict))
f.close()
print('filename:','dump'+t+'.json',file=sys.stderr)
print('dump'+t+'.json')
import json
import re
import traceback
import sys
regex = re.compile('!!REPLACE!!(.+?)!!REPLACE!!')
file = open('dmp.json','r')
mapping = json.loads(file.read())
file.close()
for fname in mapping.keys():
try:
f = open(fname,'r')
content = f.read()
f.close()
f = open(fname,'w')
result = content
for url in regex.finditer(content):
result = result.replace(url.group(0),'#'+mapping[url.group(1).replace('html','md')])
f.write(result)
f.close()
except Exception as e:
print('failed processing',fname,file=sys.stderr)
print(traceback.format_exc(),file=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment