Skip to content

Commit fe24d6f

Browse files
Add useful_scripts/chm2html.py from AskUbuntu.com, where I...
...first published it in 2018 here: https://askubuntu.com/a/1021427/327339
1 parent f1dc525 commit fe24d6f

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

useful_scripts/chm2html.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/python3
2+
3+
# This file is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
4+
5+
"""
6+
chm2html.py
7+
- convert .chm files to .html, using the command shown here, with a few extra features (folder names, shortcuts, etc):
8+
http://www.ubuntugeek.com/how-to-convert-chm-files-to-html-or-pdf-files.html
9+
- (this is my first ever python shell script to be used as a bash replacement)
10+
11+
Gabriel Staples
12+
www.ElectricRCAircraftGuy.com
13+
Written: 2 Apr. 2018
14+
Updated: 2 Apr. 2018
15+
16+
INSTALLATION INSTRUCTIONS:
17+
- See my answer here: https://askubuntu.com/a/1021427/327339
18+
1. Create a `~/bin` directory if you don't already have one:
19+
mkdir ~/bin
20+
2. Make a symlink to chm2html.py in your `~/bin` directory:
21+
ln -s ~/path/to/chm2html.py ~/bin/chm2html
22+
3. Log out of Ubuntu then log back in, or reload your paths with:
23+
source ~/.bashrc
24+
4. Use it! `chm2html myFile.chm`. This automatically converts the .chm file and places the .html
25+
files into a new folder called ./myFile, then it creates a symlink called ./myFile_index.html which
26+
points to ./myFile/index.html.
27+
28+
References:
29+
- ***** [MY OWN ANSWER with this code] https://askubuntu.com/a/1021427/327339
30+
- http://www.ubuntugeek.com/how-to-convert-chm-files-to-html-or-pdf-files.html
31+
- format: `extract_chmLib book.chm outdir`
32+
- http://www.linuxjournal.com/content/python-scripts-replacement-bash-utility-scripts
33+
- http://www.pythonforbeginners.com/system/python-sys-argv
34+
35+
USAGE/Python command format: `./chm2html.py fileName.chm`
36+
- make a symbolic link to this target in ~/bin: `ln -s ~/GS/dev/shell_scripts-Linux/chm2html/chm2html.py ~/bin/chm2html`
37+
- Now you can call `chm2html file.chm`
38+
- This will automatically convert the fileName.chm file to .html files by creating a fileName directory where you are,
39+
then it will also create a symbolic link right there to ./fileName/index.html, with the symbolic link name being
40+
fileName_index.html
41+
42+
"""
43+
44+
45+
import sys, os
46+
47+
if __name__ == "__main__":
48+
# print("argument = " + sys.argv[1]); # print 1st argument; DEBUGGING
49+
# print(len(sys.argv)) # DEBUGGING
50+
51+
# get file name from input parameter
52+
if (len(sys.argv) <= 1):
53+
print("Error: missing .chm file input parameter. \n"
54+
"Usage: `./chm2html.py fileName.chm`. \n"
55+
"Type `./chm2html -h` for help. `Exiting.")
56+
sys.exit()
57+
58+
if (sys.argv[1]=="-h" or sys.argv[1]=="h" or sys.argv[1]=="help" or sys.argv[1]=="-help"):
59+
print("Usage: `./chm2html.py fileName.chm`. This will automatically convert the fileName.chm file to\n"
60+
".html files by creating a directory named \"fileName\" right where you are, then it will also create a\n"
61+
"symbolic link in your current folder to ./fileName/index.html, with the symbolic link name being fileName_index.html")
62+
sys.exit()
63+
64+
file = sys.argv[1] # Full input parameter (fileName.chm)
65+
name = file[:-4] # Just the fileName part, withOUT the extension
66+
extension = file[-4:]
67+
if (extension != ".chm"):
68+
print("Error: Input parameter must be a .chm file. Exiting.")
69+
sys.exit()
70+
71+
# print(name) # DEBUGGING
72+
# Convert the .chm file to .html
73+
command = "extract_chmLib " + file + " " + name
74+
print("Command: " + command)
75+
os.system(command)
76+
77+
# Make a symbolic link to ./name/index.html now
78+
pwd = os.getcwd()
79+
target = pwd + "/" + name + "/index.html"
80+
# print(target) # DEBUGGING
81+
# see if target exists
82+
if (os.path.isfile(target) == False):
83+
print("Error: \"" + target + "\" does not exist. Exiting.")
84+
sys.exit()
85+
# make link
86+
ln_command = "ln -s " + target + " " + name + "_index.html"
87+
print("Command: " + ln_command)
88+
os.system(ln_command)
89+
90+
print("Operation completed successfully.")

0 commit comments

Comments
 (0)