diff options
author | Andrei Karas <akaras@inbox.ru> | 2016-04-11 20:00:13 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2016-04-11 20:19:46 +0300 |
commit | eab2c28c85ce95a6afd64ba25e9aeaa33df0f471 (patch) | |
tree | 2c25912e870608caa5176a46562240aeda17b4e1 | |
download | pagesindexgen-eab2c28c85ce95a6afd64ba25e9aeaa33df0f471.tar.gz pagesindexgen-eab2c28c85ce95a6afd64ba25e9aeaa33df0f471.tar.bz2 pagesindexgen-eab2c28c85ce95a6afd64ba25e9aeaa33df0f471.tar.xz pagesindexgen-eab2c28c85ce95a6afd64ba25e9aeaa33df0f471.zip |
Initial commit.
-rwxr-xr-x | pagesindexgen.py | 124 | ||||
-rw-r--r-- | templates/default/back.tpl | 1 | ||||
-rw-r--r-- | templates/default/dir.tpl | 1 | ||||
-rw-r--r-- | templates/default/file.tpl | 1 | ||||
-rw-r--r-- | templates/default/index.tpl | 8 |
5 files changed, 135 insertions, 0 deletions
diff --git a/pagesindexgen.py b/pagesindexgen.py new file mode 100755 index 0000000..9a0e587 --- /dev/null +++ b/pagesindexgen.py @@ -0,0 +1,124 @@ +#! /usr/bin/env python3 +# -*- coding: utf8 -*- +# +# Copyright (C) 2016 Andrei Karas (4144) + +import os +import re +import sys + +filt = re.compile(".+", re.IGNORECASE) +indexName = "index.htm" + +def showHelp(): + print("PagesIndexGen v0.1") + print("Usage:") + print(" ./pagesindexgen.py filedir sitedir template") + print("Example:") + print(" ./pagesindexgen.py") + print(" ./pagesindexgen.py .") + print(" ./pagesindexgen.py . /") + print(" ./pagesindexgen.py . / default") + +def readTemplate(name): + with open(template + os.path.sep + name, "r") as f: + return f.read() + +def saveTemplate(name, data): + with open(name + os.path.sep + indexName, "w") as w: + w.write(data) + +def getFullRelativePath(relativeDir, fileName): + if len(relativeDir) > 0: + relativePath = relativeDir + os.path.sep + fileName + else: + relativePath = fileName + return relativePath + +def buildIndex(curDir, relativeDir, dirName, dirs, files, backLink): + indexTemplate = readTemplate("index.tpl") + dirTemplate = readTemplate("dir.tpl") + fileTemplate = readTemplate("file.tpl") + + data = "" + + if backLink == True: + backTemplate = readTemplate("back.tpl") + data = data + backTemplate.format( + name = "..", + url = ".." + os.path.sep + indexName, + sitedir = dirName, + urldir = relativeDir, + filedir = curDir, + ) + + for path in dirs: + data = data + dirTemplate.format( + name = path, + url = path + os.path.sep + indexName, + sitedir = dirName, + urldir = relativeDir, + filedir = curDir, + ) + + for name in files: + fullPath = os.path.abspath(curDir + os.path.sep + name) + relativePath = getFullRelativePath(relativeDir, name) + data = data + fileTemplate.format( + name = name, + url = name, + sitedir = dirName, + urldir = relativeDir, + filedir = curDir, + ) + + saveTemplate(curDir, indexTemplate.format( + sitedir = dirName, + url = relativeDir, + filedir = curDir, + body = data + )) + +def processDir(curDir, relativeDir, dirName, backLink): + curFiles = os.listdir(curDir) + dirs = [] + files = [] + for fileName in curFiles: + if fileName[0] == "." or fileName == indexName: + continue + fullPath = os.path.abspath(curDir + os.path.sep + fileName) + relativePath = getFullRelativePath(relativeDir, fileName) + if not os.path.isfile(fullPath): + dirs.append(fileName) + elif filt.search(fileName): + files.append(fileName) + + dirs.sort() + files.sort() + + buildIndex(curDir, relativeDir, dirName, dirs, files, backLink) + + for path in dirs: + fullPath = os.path.abspath(curDir + os.path.sep + path) + relativePath = getFullRelativePath(relativeDir, path) + processDir(fullPath, relativePath, path, True) + +num = len(sys.argv) +if num < 1 or num > 4: + showHelp() + exit(1) +if num > 1: + filePath = sys.argv[1] +else: + filePath = "." +if num > 2: + sitePath = sys.argv[2] +else: + sitePath = "" +if num > 3: + template = sys.argv[3] +else: + template = "default" +template = "templates" + os.path.sep + template + +processDir(filePath, sitePath, "", False) diff --git a/templates/default/back.tpl b/templates/default/back.tpl new file mode 100644 index 0000000..a6004fd --- /dev/null +++ b/templates/default/back.tpl @@ -0,0 +1 @@ +<a href="{url}">{name}</a><br/> diff --git a/templates/default/dir.tpl b/templates/default/dir.tpl new file mode 100644 index 0000000..a6004fd --- /dev/null +++ b/templates/default/dir.tpl @@ -0,0 +1 @@ +<a href="{url}">{name}</a><br/> diff --git a/templates/default/file.tpl b/templates/default/file.tpl new file mode 100644 index 0000000..4b660f7 --- /dev/null +++ b/templates/default/file.tpl @@ -0,0 +1 @@ +<a href="{name}">{name}</a><br/> diff --git a/templates/default/index.tpl b/templates/default/index.tpl new file mode 100644 index 0000000..8549bd4 --- /dev/null +++ b/templates/default/index.tpl @@ -0,0 +1,8 @@ +<!DOCTYPE html> +<html> +<title>{url} - {sitedir}</title> +<body> +{body} +<!-- created by pagesindexgen https://gitlab.com/4144/pagesindexgen --> +</body> +</html>
\ No newline at end of file |