diff --git a/.gitignore b/.gitignore index eb0a0b1..cdf363a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +#test result +tests/result* +tests/result* #vim session files *.vims diff --git a/ASCIIsite.py b/ASCIIsite.py index cd825c0..f0978b5 100755 --- a/ASCIIsite.py +++ b/ASCIIsite.py @@ -11,9 +11,18 @@ 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.') + parser.add_argument('-v', '--verbose', action='store_true', help='outputs debug messages onto the console.') args=parser.parse_args() + #setting log level + if args.verbose: + logging.info('setting log level to verbose') + logging.getLogger().setLevel(level=logging.DEBUG) + #set compress flag if args.output != None and not args.compress: #detect based on whether outFile has a .tar.gz filename. @@ -43,15 +52,34 @@ def parse_arguments(): logging.info(f'outputting to {outFile.resolve()}') logging.debug(f'compress is {compress}') - return args.inputDir.resolve(), outFile, compress + 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) + + if not args.inputDir.resolve().exists(): + print(f'Inputdir {args.inputDir.resolve()} does not exist!') + exit() + + 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: - def __init__(self, srcDir): + def __init__(self, srcDir, exclude): self.tmpDir=tempfile.TemporaryDirectory() logging.debug(f'making tmp file from {srcDir} at {self.tmpDir.name}') self.path=self.tmpDir.name+'/'+Path(srcDir).resolve().name - self.ignorePattern=shutil.ignore_patterns('*.adoc', '.git', '.gitignore') + self.ignorePatterns=['*.adoc', '.gitignore', '.git/*'] + self.ignorePatterns.extend(exclude) + self.ignorePattern=shutil.ignore_patterns(*self.ignorePatterns) shutil.copytree(srcDir, self.path, ignore=self.ignorePattern, symlinks=False) #copy out from tmpDir (which may be in RAM, depending on distrubution) to disk @@ -73,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=parse_arguments() + inFile, outFile, stylesheet, compress, exclude=parse_arguments() os.chdir(inFile) - tmpDir=TmpDir('./') + 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 e95c43b..f4d2562 100644 --- a/README.md +++ b/README.md @@ -3,17 +3,26 @@ ![Licence](https://img.shields.io/badge/Licence-GPL-blue) ## What is it? -ASCIIsite is a simple, barebones static site generator. You give it a directory contaning asciidoctor documents and supporing media in the strucutre you want your site to be in, and it spits out a fully functional static site based on that input directory. +ASCIIsite is a simple, bare bones static site generator. You give it a directory containing asciidoctor documents and supporting media in the structure you want your site to be in, and it spits out a fully functional static site based on that input directory. ## Usage -ASCIISite takes 2 (so far) optional arguments followed by the single mandatory arument 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 --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. @@ -23,13 +32,12 @@ Say you have a nice asciidoctor directory like this: ``` test ├── dir -│ ├── collatz.py │ └── subdir │ └── linked.adoc ├── images │ └── test_pattern.svg ├── include -│ └── include.adoc +│ └── include.txt └── landing_page.adoc ``` @@ -37,26 +45,67 @@ 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 +│ └── subdir +│ └── linked.html +├── include +│ └── include.txt +├── images +│ └── test_pattern.svg +└── landing_page.html +``` + +If, say, the include directory is a directory needed for the asciidoc compilation, +but not needed for the final website, you can use the --exclude option to specify a list of glob patterns to exclude. For example, +```` +ASCIIsite.py --exclude 'include*' -o output test +``` + +will get you an output like: +``` +output +├── css +│ └── asciidoctor.css ├── dir -│ ├── collatz.py │ └── subdir │ └── linked.html ├── images │ └── test_pattern.svg +└── landing_page.html +``` + +and, to use your custom stylesheet named `+myTheme.css+`, you can use: + +``` +ASCIIsite.py --stylesheet myTheme.css -o output test +``` + +``` +output +├── css +│ └── myTheme.css +├── dir +│ └── subdir +│ └── linked.html ├── include -│ └── include.html +│ └── include.txt +├── images +│ └── test_pattern.svg └── landing_page.html ``` Alternatively, you can run + ``` -ASCIISite.py -z -o result test +ASCIISite.py -z -o output test ``` -to get a .tar.gz file containing the result directory. +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 92% rename from tests/result/dir/subdir/linked.html rename to tests/control/css/asciidoctor.css index 31be3d8..98f27c4 100644 --- a/tests/result/dir/subdir/linked.html +++ b/tests/control/css/asciidoctor.css @@ -1,14 +1,3 @@ - - -
- - - - - -+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. ++
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.+
+ + | ++Test the links in this page! + | +
Lorem ipsum dolor sit amet, consectetur adipiscing elit. +Phasellus mi tellus, suscipit in dolor nec, faucibus aliquam nulla. +Mauris gravida, felis commodo molestie tincidunt, sem erat varius lorem, nec lobortis eros sem ut augue. +Donec suscipit tristique imperdiet. +Sed maximus est ultrices urna imperdiet, vitae facilisis eros posuere. +Quisque a mollis mauris. +Pellentesque fermentum, libero sed sollicitudin interdum, dolor nunc euismod purus, et laoreet tellus augue vitae turpis. +Pellentesque condimentum elit quis semper tincidunt. +Nullam consectetur euismod accumsan. +In dictum nibh ut iaculis euismod. +Phasellus ut nibh non ipsum volutpat cursus. +Sed eleifend, enim vitae ultrices auctor, dolor ipsum molestie dui, id dignissim massa neque a mauris.
+bold test
+italics test
+'monospace test'
+'monospace bold test'
+'monospace italic test'
+*italic bold test*
+'all three test'
+*escaped bold test*
+underline test using html passthrough test
+*escaped bold test with plus*
+→ ⇒ ← ⇐
+==colapsable block
+== list tests
+Lorum
+ipsum
+dolor
+sit
+amet
+consectetur
+Lorum
+ipsum
+dolor
+sit
+amet
+consectetur
+Lorum
+ipsum
+dolor
+sit
+amet
+consectetur
+Lorum
+ipsum
+dolor
+sit
+amet
+consectetur
+adipiscing
+elit
+\(\frac{\frac{1}{x}+\frac{1}{y}}{y-z}\)
++test + | +
+ to make sure software works correctly + |
+
+git + | +
+ a gift from the machine spirits + |
+
+printers + | +
+ the greatest evil to ever befall IT. + |
+
Lets go back to the [list tests] or to the tables
+def nextInSequence(number):
+ if isinstance(number, int):
+ if number % 2 == 0:
+ return number // 2
+ else:
+ return 3*number+1
+ else:
+ raise TypeError('input must be int!')
+
+def seqenceLength(number):
+ length = 0
+ while number != 1:
+ number = nextInSequence(number)
+ length += 1
+ return length
+Name | +Group | +Description | +
---|---|---|
Firefox |
+Web Browser |
+
+
+Mozilla Firefox is an open-source web browser. +It’s designed for: +
+
|
+
Ruby |
+Programming Language |
+A programmer’s best friend. |
+
Fusce maximus nec magna eu ultricies. +Fusce quis tellus vitae arcu facilisis lobortis. +Donec id erat at enim porta placerat in vitae sapien. +Duis justo arcu, hendrerit nec nulla eu, dictum dapibus ipsum. +Sed fermentum id elit eget fringilla. +Suspendisse volutpat imperdiet justo, ut efficitur odio maximus et. +Nunc interdum sollicitudin eros sit amet convallis. +Praesent volutpat tempus metus id tincidunt. +Proin aliquet justo a fermentum consectetur. +Nunc scelerisque, nisi id scelerisque dictum, nibh lectus ultrices nunc, quis ultricies erat velit sit amet urna. +Maecenas orci felis, volutpat at bibendum ut, mattis eu justo.
+example of _listing block_ +with verbatim line break+
example block +with line break
+_literal_ block +with line break+
++++quote block +with line break
+
+ + | +
+
+
+This is an example of an admonition block. +
+
+Unlike an admonition paragraph, it may contain any AsciiDoc content. +The style can be any one of the admonition labels: +
+
+
|
+
+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. ++
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.+
+ + | ++Test the links in this page! + | +
Lorem ipsum dolor sit amet, consectetur adipiscing elit. +Phasellus mi tellus, suscipit in dolor nec, faucibus aliquam nulla. +Mauris gravida, felis commodo molestie tincidunt, sem erat varius lorem, nec lobortis eros sem ut augue. +Donec suscipit tristique imperdiet. +Sed maximus est ultrices urna imperdiet, vitae facilisis eros posuere. +Quisque a mollis mauris. +Pellentesque fermentum, libero sed sollicitudin interdum, dolor nunc euismod purus, et laoreet tellus augue vitae turpis. +Pellentesque condimentum elit quis semper tincidunt. +Nullam consectetur euismod accumsan. +In dictum nibh ut iaculis euismod. +Phasellus ut nibh non ipsum volutpat cursus. +Sed eleifend, enim vitae ultrices auctor, dolor ipsum molestie dui, id dignissim massa neque a mauris.
+bold test
+italics test
+'monospace test'
+'monospace bold test'
+'monospace italic test'
+*italic bold test*
+'all three test'
+*escaped bold test*
+underline test using html passthrough test
+*escaped bold test with plus*
+→ ⇒ ← ⇐
+==colapsable block
+== list tests
+Lorum
+ipsum
+dolor
+sit
+amet
+consectetur
+Lorum
+ipsum
+dolor
+sit
+amet
+consectetur
+Lorum
+ipsum
+dolor
+sit
+amet
+consectetur
+Lorum
+ipsum
+dolor
+sit
+amet
+consectetur
+adipiscing
+elit
+\(\frac{\frac{1}{x}+\frac{1}{y}}{y-z}\)
++test + | +
+ to make sure software works correctly + |
+
+git + | +
+ a gift from the machine spirits + |
+
+printers + | +
+ the greatest evil to ever befall IT. + |
+
Lets go back to the [list tests] or to the tables
+def nextInSequence(number):
+ if isinstance(number, int):
+ if number % 2 == 0:
+ return number // 2
+ else:
+ return 3*number+1
+ else:
+ raise TypeError('input must be int!')
+
+def seqenceLength(number):
+ length = 0
+ while number != 1:
+ number = nextInSequence(number)
+ length += 1
+ return length
+Name | +Group | +Description | +
---|---|---|
Firefox |
+Web Browser |
+
+
+Mozilla Firefox is an open-source web browser. +It’s designed for: +
+
|
+
Ruby |
+Programming Language |
+A programmer’s best friend. |
+
Fusce maximus nec magna eu ultricies. +Fusce quis tellus vitae arcu facilisis lobortis. +Donec id erat at enim porta placerat in vitae sapien. +Duis justo arcu, hendrerit nec nulla eu, dictum dapibus ipsum. +Sed fermentum id elit eget fringilla. +Suspendisse volutpat imperdiet justo, ut efficitur odio maximus et. +Nunc interdum sollicitudin eros sit amet convallis. +Praesent volutpat tempus metus id tincidunt. +Proin aliquet justo a fermentum consectetur. +Nunc scelerisque, nisi id scelerisque dictum, nibh lectus ultrices nunc, quis ultricies erat velit sit amet urna. +Maecenas orci felis, volutpat at bibendum ut, mattis eu justo.
+example of _listing block_ +with verbatim line break+
example block +with line break
+_literal_ block +with line break+
++++quote block +with line break
+
+ + | +
+
+
+This is an example of an admonition block. +
+
+Unlike an admonition paragraph, it may contain any AsciiDoc content. +The style can be any one of the admonition labels: +
+
+
|
+
Fusce maximus nec magna eu ultricies. -Fusce quis tellus vitae arcu facilisis lobortis. -Donec id erat at enim porta placerat in vitae sapien. -Duis justo arcu, hendrerit nec nulla eu, dictum dapibus ipsum. -Sed fermentum id elit eget fringilla. -Suspendisse volutpat imperdiet justo, ut efficitur odio maximus et. -Nunc interdum sollicitudin eros sit amet convallis. -Praesent volutpat tempus metus id tincidunt. -Proin aliquet justo a fermentum consectetur. -Nunc scelerisque, nisi id scelerisque dictum, nibh lectus ultrices nunc, quis ultricies erat velit sit amet urna. -Maecenas orci felis, volutpat at bibendum ut, mattis eu justo.
-example of _listing block_ -with verbatim line break-
example block -with line break
-_literal_ block -with line break-
----quote block -with line break
-
- Note
- |
-
-
-
-This is an example of an admonition block. -
-
-Unlike an admonition paragraph, it may contain any AsciiDoc content. -The style can be any one of the admonition labels: -
-
-
|
-
- - | --Test the links in this page! - | -
Lorem ipsum dolor sit amet, consectetur adipiscing elit. -Phasellus mi tellus, suscipit in dolor nec, faucibus aliquam nulla. -Mauris gravida, felis commodo molestie tincidunt, sem erat varius lorem, nec lobortis eros sem ut augue. -Donec suscipit tristique imperdiet. -Sed maximus est ultrices urna imperdiet, vitae facilisis eros posuere. -Quisque a mollis mauris. -Pellentesque fermentum, libero sed sollicitudin interdum, dolor nunc euismod purus, et laoreet tellus augue vitae turpis. -Pellentesque condimentum elit quis semper tincidunt. -Nullam consectetur euismod accumsan. -In dictum nibh ut iaculis euismod. -Phasellus ut nibh non ipsum volutpat cursus. -Sed eleifend, enim vitae ultrices auctor, dolor ipsum molestie dui, id dignissim massa neque a mauris.
-bold test
-italics test
-'monospace test'
-'monospace bold test'
-'monospace italic test'
-*italic bold test*
-'all three test'
-*escaped bold test*
-underline test using html passthrough test
-*escaped bold test with plus*
-→ ⇒ ← ⇐
-Lorum
-ipsum
-dolor
-sit
-amet
-consectetur
-Lorum
-ipsum
-dolor
-sit
-amet
-consectetur
-Lorum
-ipsum
-dolor
-sit
-amet
-consectetur
-Lorum
-ipsum
-dolor
-sit
-amet
-consectetur
-adipiscing
-elit
--test - | -
- to make sure software works correctly - |
-
-git - | -
- a gift from the machine spirits - |
-
-printers - | -
- the greatest evil to ever befall IT. - |
-
Lets go back to the list tests or to the tables
-def nextInSequence(number):
- if isinstance(number, int):
- if number % 2 == 0:
- return number // 2
- else:
- return 3*number+1
- else:
- raise TypeError('input must be int!')
-
-def seqenceLength(number):
- length = 0
- while number != 1:
- number = nextInSequence(number)
- length += 1
- return length
-Name | -Group | -Description | -
---|---|---|
Firefox |
-Web Browser |
-
-
-Mozilla Firefox is an open-source web browser. -It’s designed for: -
-
|
-
Ruby |
-Programming Language |
-A programmer’s best friend. |
-
Fusce maximus nec magna eu ultricies. -Fusce quis tellus vitae arcu facilisis lobortis. -Donec id erat at enim porta placerat in vitae sapien. -Duis justo arcu, hendrerit nec nulla eu, dictum dapibus ipsum. -Sed fermentum id elit eget fringilla. -Suspendisse volutpat imperdiet justo, ut efficitur odio maximus et. -Nunc interdum sollicitudin eros sit amet convallis. -Praesent volutpat tempus metus id tincidunt. -Proin aliquet justo a fermentum consectetur. -Nunc scelerisque, nisi id scelerisque dictum, nibh lectus ultrices nunc, quis ultricies erat velit sit amet urna. -Maecenas orci felis, volutpat at bibendum ut, mattis eu justo.
-example of _listing block_ -with verbatim line break-
example block -with line break
-_literal_ block -with line break-
----quote block -with line break
-
- - | -
-
-
-This is an example of an admonition block. -
-
-Unlike an admonition paragraph, it may contain any AsciiDoc content. -The style can be any one of the admonition labels: -
-
-
|
-