#!/usr/bin/env python # ************************************************************************ # * Copyright (c) 2022 Yorik van Havre * # * * # * This program is free software; you can redistribute it and/or modify * # * it under the terms of the GNU Lesser General Public License (LGPL) * # * as published by the Free Software Foundation; either version 2 of * # * the License, or (at your option) any later version. * # * for detail see the LICENCE text file. * # * * # * This program is distributed in the hope that it will be useful, * # * but WITHOUT ANY WARRANTY; without even the implied warranty of * # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * # * GNU Library General Public License for more details. * # * * # * You should have received a copy of the GNU Library General Public * # * License along with this program; if not, write to the Free Software * # * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * # * USA * # * * # ************************************************************************ """This script produces an index.html file and an images/ folder which shows and allows to download the contents of the library""" import os import zipfile import hashlib import urllib.request import urllib.parse # path definitions homefolder = os.path.abspath(os.curdir) imagefolder = "thumbnails" htmlfile = os.path.join(homefolder, "index.html") baseurl = "https://github.com/FreeCAD/FreeCAD-library/blob/master/" excludelist = ["thumbnails"] # icons defaulticon = os.path.join(imagefolder, "freecad-document.svg") gridicon = os.path.join(imagefolder, "icon-grid.svg") listicon = os.path.join(imagefolder, "icon-list.svg") stepicon = os.path.join(imagefolder, "icon-grey.svg") brepicon = os.path.join(imagefolder, "icon-blue.svg") stlicon = os.path.join(imagefolder, "icon-green.svg") collapseicon = os.path.join(imagefolder, "icon-right.svg") expandicon = os.path.join(imagefolder, "icon-down.svg") # html template template = """ FreeCAD Library """ def build_html(dirpath, level=1): """walks a directory and builds cards from its contents""" html = "" if os.path.isdir(dirpath): html += build_title(dirpath, level) if level > 1: offset = 5 + (level - 2) * 2 html += '\n' return html def build_title(dirpath, level): """builds an html title from a path""" if level == 1: # do not print the first-level title return "" sl = str(level) sn = '' sn += os.path.basename(dirpath) if level < 7: title = '' title += sn + '\n' else: title = '
' title += sn + '
\n' return title def build_card(filepath): """builds an HTML card for a given file""" print("Building card for", filepath) html = "" if os.path.exists(filepath): basename = os.path.splitext(filepath)[0] name = os.path.basename(basename) iconpath = get_icon(filepath) raw = "?raw=true" fileurl = baseurl + clean_path(filepath) + raw html += '
' html += '' html += '' html += '
' + name + '
' html += '
' html += '' # links html += '
\n' # card return html def get_icon(filepath): """returns a thumbnail image path for a given file path""" iconname = get_hashname(filepath) iconurl = os.path.join(imagefolder, iconname) iconpath = os.path.join(homefolder, iconurl) try: zfile = zipfile.ZipFile(filepath) except Exception: return defaulticon if "thumbnails/Thumbnail.png" in zfile.namelist(): data = zfile.read("thumbnails/Thumbnail.png") thumb = open(iconpath, "wb") thumb.write(data) thumb.close() else: return defaulticon if not os.path.exists(iconpath): return defaulticon return iconurl def get_hashname(filepath): """creates a png filename for a given file path""" filepath = clean_path(filepath) return hashlib.md5(filepath.encode()).hexdigest()+".png" def clean_path(filepath): """cleans a file path into subfolder/subfolder/file form""" if filepath.startswith(homefolder): # strip local part od the path filepath = filepath[len(homefolder):] filepath = filepath.replace("\\", "/") if filepath.startswith("/"): filepath = filepath[1:] filepath = urllib.parse.quote(filepath) return filepath if __name__ == "__main__": html = build_html(homefolder) html = template.replace("", html) html = html.replace("", listicon) html = html.replace("", gridicon) html = html.replace("", collapseicon) html = html.replace("", expandicon) with open(htmlfile, "w") as index: index.write(html) print("Saving", htmlfile, "... All done!")