diff --git a/.gitignore b/.gitignore index d793de9..cdf363a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ #test result -result/* +tests/result* +tests/result* #vim session files *.vims diff --git a/ASCIIsite.py b/ASCIIsite.py index 3277b64..f0978b5 100755 --- a/ASCIIsite.py +++ b/ASCIIsite.py @@ -11,6 +11,7 @@ def parse_arguments(): parser=argparse.ArgumentParser(description='create a website directory structure by converting .adoc files in a directory strucutre to .html files.') parser.add_argument('inputDir', type=Path, help='The directory of adoc files to be copied and converted.') parser.add_argument('-o', '--output', type=Path, help='What to name the generated directory or tar file') + parser.add_argument('--stylesheet', type=Path, help='A custom CSS file to be applied to the output.') parser.add_argument('--exclude-file', type=Path, help='A text file containing glob patterns to exclude, 1 per line.') parser.add_argument('--exclude', nargs='+', help='A list of glob patterns to ignore. Remember to quote them so your shell doesnt escape them!') parser.add_argument('-z', '--compress', action='store_true', help='whether to compress the resulting directory to a tar.gz file. can be usefull for scripting to transfer the site to a remote server.') @@ -51,21 +52,24 @@ def parse_arguments(): logging.info(f'outputting to {outFile.resolve()}') logging.debug(f'compress is {compress}') - try: + exclude=[] + if args.exclude_file != None: with open(args.exclude_file, 'r') as file: exclude=[glob.strip() for glob in file] - if args.exclude != None: - exclude.extend(args.exclude) - except Exception as e: - print(str(e)) - exit() + if args.exclude != None: + exclude.extend(args.exclude) if not args.inputDir.resolve().exists(): print(f'Inputdir {args.inputDir.resolve()} does not exist!') exit() - return args.inputDir.resolve(), outFile, compress, exclude + stylesheet=None + if args.stylesheet != None: + stylesheet =args.stylesheet.resolve() + logging.info(f'using stylesheet {stylesheet}') + + return args.inputDir.resolve(), outFile, stylesheet, compress, exclude #Doing it in a tmpDir first, as some distrubutions put temp files on a ramdisk. this should speed up the operation sigificantly. class TmpDir: @@ -97,32 +101,55 @@ class TmpDir: def find_paths_to_convert(fileNameGlob): return glob.glob(f'**/{fileNameGlob}', recursive=True) +#finds the depth of a file relative to given directory +def find_relative_file_depth (subfile, parentDir): + subfile=Path(subfile).resolve() + parentDir=Path(parentDir).resolve() + return len(subfile.parts)-len(parentDir.parts)-1 + #simple wrapper around the asciidoctor cli. -def convert_file(inDir, outDir, inFile): +def convert_file(inDir: Path, outDir: Path, inFile: Path, stylesheet: Path): + #in order for the stylesdir and imagesdir to be linked to correctly, we need to know the relative depth between the two directories. + depth=find_relative_file_depth(inFile, inDir) + logging.info(f'converting {Path(inFile).resolve()}') - logging.debug(f'converting {inFile} from directory {inDir} to directory {outDir}') - try: - #the destdir can be used instead of destfile in order to preserve the directory structure relative to the base dir. really useful. - subprocess.run(['asciidoctor', + logging.debug(f'converting {inFile=}, {outDir=}, {inDir=}, {stylesheet=}') + + depthstring= '../'*depth + + arguments=['asciidoctor', + #makes the stylesheet linked, but still includes it in the output. + '--attribute=linkcss', + f'--attribute=stylesdir={depthstring}css', + #set imagesdir + f'--attribute=imagesdir={depthstring}images', #specifies the source directory root. f'--source-dir={inDir}', #Destination dir. It takes the file from the subtree --source-dir and puts it in the equivilant location in the subtree --destination-dir. (talking about filesystem subtrees). f'--destination-dir={outDir}', - inFile], - check=True) + inFile] + + if stylesheet != None: + arguments.insert(1, f'--attribute=copycss={stylesheet}') + arguments.insert(1, f'--attribute=stylesheet={stylesheet.name}') + else: + arguments.insert(1, f'--attribute=copycss') + logging.debug(f'{arguments=}') + try: + #the destdir can be used instead of destfile in order to preserve the directory structure relative to the base dir. really useful. + subprocess.run(arguments, check=True) except Exception as e: logging.error(f'could not convert {inFile}!') - logging.error(f'stdErr was {e.stderr}') - logging.error(f'stdOut was {e.stdout}') + logging.error(f'{e}') if __name__ == '__main__': - inFile, outFile, compress, exclude=parse_arguments() + inFile, outFile, stylesheet, compress, exclude=parse_arguments() os.chdir(inFile) tmpDir=TmpDir('./', exclude) pathsToConvert=find_paths_to_convert('*.adoc') for i in pathsToConvert: - convert_file('./', tmpDir.path, i) + convert_file(inDir='./', outDir=tmpDir.path, inFile=i, stylesheet=stylesheet) if compress: tmpDir.compress_and_copy_self_to(outFile) diff --git a/README.md b/README.md index 464838f..f4d2562 100644 --- a/README.md +++ b/README.md @@ -7,21 +7,22 @@ ASCIIsite is a simple, bare bones static site generator. You give it a directory ## Usage -ASCIISite takes 2 (so far) optional arguments followed by the single mandatory argument telling it what directory to convert. +ASCIISite several optional arguments followed by a single mandatory argument telling it what directory to convert. the -o or --output option simply tells ASCIISite what to name the output file. the -z or --compress flag tells ASCIISite to put the final product in a compressed tar.gz file as its output. This is especially useful if you are running ASCIISite on your personal computer, and will be uploading the tar.gz file to your server. -the --exclude flag allows you to specify a list of glob patterns. Any file matching these glob patterns will not be copied to the output. +The --sylesheet options allows you to set a custom stylesheet to use instead of the default ASCIIDoctor stylesheet. + +The --exclude flag allows you to specify a list of glob patterns. Any file matching these glob patterns will not be copied to the output. The --exclude-file flag allows you to specify a file containing one glob to exclude per line. Other than inputting from a file, works exactly the same as --exclude. Note that it cannot parse the full spec of .gitignore files, only traditional globs. Exclusions are helpful for any files that are needed for the compilation of the asciidoc files, but do not need to be in the final site. The main use case I am aware of is files that are put into an asciidoc document via an include statement. - As for how to format the input directory, thats up to you. The directory structure of the input will be mirrored in the structure of the output website. The only real rule you need to follow is that all your links to other pages in the input directory should be relative, so they dont get broken when you move the output directory around. @@ -31,7 +32,6 @@ Say you have a nice asciidoctor directory like this: ``` test ├── dir -│   ├── collatz.py │   └── subdir │   └── linked.adoc ├── images @@ -45,20 +45,21 @@ Where some pages link to others, some pages include others, and some pages have You can run ``` -ASCIISite.py -o result test +ASCIISite.py -o output test ``` to get a file tree like: ``` -result +output +├── css +│   └── asciidoctor.css ├── dir -│   ├── collatz.py │   └── subdir │   └── linked.html -├── images -│   └── test_pattern.svg ├── include │   └── include.txt +├── images +│   └── test_pattern.svg └── landing_page.html ``` @@ -70,9 +71,10 @@ ASCIIsite.py --exclude 'include*' -o output test will get you an output like: ``` -result +output +├── css +│   └── asciidoctor.css ├── dir -│   ├── collatz.py │   └── subdir │   └── linked.html ├── images @@ -80,9 +82,30 @@ result └── landing_page.html ``` -Alternatively, you can run +and, to use your custom stylesheet named `+myTheme.css+`, you can use: + ``` -ASCIISite.py -z -o result test +ASCIIsite.py --stylesheet myTheme.css -o output test ``` -to get a .tar.gz file containing the result directory. +``` +output +├── css +│   └── myTheme.css +├── dir +│   └── subdir +│   └── linked.html +├── include +│   └── include.txt +├── images +│   └── test_pattern.svg +└── landing_page.html +``` + +Alternatively, you can run + +``` +ASCIISite.py -z -o output test +``` + +to get a .tar.gz file containing the output directory. diff --git a/tests/result/dir/subdir/linked.html b/tests/control/css/asciidoctor.css similarity index 89% rename from tests/result/dir/subdir/linked.html rename to tests/control/css/asciidoctor.css index 1a4a433..98f27c4 100644 --- a/tests/result/dir/subdir/linked.html +++ b/tests/control/css/asciidoctor.css @@ -1,14 +1,3 @@ - - - - - - - - -relatively linked doc - - - - - -
-
-
-Stand By -
-
Figure 1. Technical Difficulties
-
-
-
-While the creative works from the 16th century can still be accessed and used by others, the data in some software programs from the 1990s is already inaccessible. Once a company that produces a certain product goes out of business, it has no simple way to uncover how its product encoded data. The code is thus lost, and the software is inaccessible. Knowledge has been destroyed. -
-
-— Lawrence Lessig
-May the Source Be With You -
-
-
-
Three Rings for the Elven-kings under the sky,
-Seven for the dwarf-lords in their halls of stone,
-Nine for Mortal Men doomed to die,
-One for the Dark Lord on his dark throne,
-In the Land of Mordor where the Shadows lie.
-One Ring to rule them all, One Ring to find them,
-One Ring to bring them all and in the darkness bind them
-In the Land of Mordor where the Shadows lie.
-
-— J.R.R. Tolkien
-The Fellowship of the Ring -
-
-
-

https://git.venberg.xyz/Gabe/adocStaticSiteGen

-
-
-
A mechanical marvel
-
- -
-
-
-

back to where you came!

-
-
- - - \ No newline at end of file +@media amzn-kf8{#header,#content,#footnotes,#footer{padding:0}} \ No newline at end of file diff --git a/tests/control/dir/subdir/linked.html b/tests/control/dir/subdir/linked.html index 1a4a433..b15754c 100644 --- a/tests/control/dir/subdir/linked.html +++ b/tests/control/dir/subdir/linked.html @@ -4,439 +4,11 @@ - + relatively linked doc - +