#!/usr/bin/python ''' Copyright 2021 Roman Beslik http://www.beroal.in.ua/ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ''' import sys from yattag import Doc def read_dict(fp): for line in fp: if len(line) >= 6 and line[0:5].isdigit() and line[5] == '\t': yield(line[6:].rstrip()) def write_page(dict0, doc, i0): (doc, tag, text, line) = doc.ttl() with tag('table'): with tag('tr'): for i10 in range(3): with tag('td'): with tag('table'): with tag('tr'): with tag('td'): line('b', str(i0+1)) for j4 in range(6): line('td', str(j4+1)) for i11 in range(2): i1 = i10*2 + i11 for i2 in range(6): for i3 in range(6): with tag('tr'): line('td', str(i1+1) + str(i2+1)\ + str(i3+1)) for i4 in range(6): with tag('td'): text(next(dict0)) def write_html(dict0, file_name_prefix): for i0 in range(6): (doc, tag, text, line) = Doc().ttl() doc.asis('') doc.asis('') with tag('html', xmlns = "http://www.w3.org/1999/xhtml"): with tag('head'): doc.stag('meta', ('http-equiv', "Content-Type"),\ content = "text/html; charset=utf-8") doc.stag('meta', ('http-equiv', "Content-Language"),\ content = "en") line('title', '') with tag('body'): write_page(dict0, doc, i0) with open(file_name_prefix + str(i0+1) + '.xhtml', 'w') as fp: fp.write(doc.getvalue()) def main(in_file, out_file_name_prefix): write_html(read_dict(in_file), out_file_name_prefix) if __name__ == "__main__": if (len(sys.argv) != 2): print('''\ This program transforms a Diceware dictionary text file into 6 HTML pages.\ Feed a dictionary text file to `stdin`, and the program will write HTML pages.\ Each HTML page will be in a separate file. The file name will be\ the concatenation of `out-file-name-prefix`, a page number,\ and a file extension.\ `out-file-name-prefix` may include a file path.\ A Diceware dictionary text file must contain exactly 7776 (`6**5`) words,\ each on a separate line after its number. Other lines are ignored.''') print('Usage: this-program out-file-name-prefix') else: main(sys.stdin, sys.argv[1])