1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import sys
import errno
from glob import glob
from pygments import highlight
from pygments.util import ClassNotFound
from pygments.lexers import guess_lexer, get_lexer_for_filename
from pygments.formatters import HtmlFormatter

def gen_index(path):
    if not path.endswith('/'):
        path += '/'
    dirs = glob(path + "/**")
    index = """
    <!DOCTYPE html>
    <html>
    <head>
        <title>Directory Listing</title>
        <style>
            html, body {
                margin: 0;
                padding: 0;
            }
            table {
                margin-top: 2rem;
                margin-left: 3rem;
            }
            .folder {
                width: 18px;
                height: 14px;
                position: relative;
                background-color: #FFC928;
                border-radius: 0 2px 2px 2px;
                box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.59);
            }

            .folder:before {
                content: '';
                width: 50%;
                height: 2px;
                border-radius: 1px 1px 0 0;
                background-color: #FE9F06;
                position: absolute;
                top: -2px;
                left: 0px;
            }

            .file-icon {
              font-family: Arial, Tahoma, sans-serif;
              font-weight: 300;
              display: inline-block;
              width: 24px;
              height: 32px;
              background: #018fef;
              position: relative;
              border-radius: 2px;
              text-align: left;
              -webkit-font-smoothing: antialiased;
            }
            .file-icon::before {
              display: block;
              content: "";
              position: absolute;
              top: 0;
              right: 0;
              width: 0;
              height: 0;
              border-bottom-left-radius: 2px;
              border-width: 5px;
              border-style: solid;
              border-color: #fff #fff rgba(255,255,255,.35) rgba(255,255,255,.35);
            }
            .file-icon::after {
              display: block;
              content: attr(data-type);
              position: absolute;
              bottom: 0;
              left: 0;
              font-size: 10px;
              color: #fff;
              text-transform: lowercase;
              width: 100%;
              padding: 2px;
          white-space: nowrap;
          overflow: hidden;
        }
        /* fileicons */
        .file-icon-xs {
          width: 12px;
          height: 16px;
          border-radius: 2px;
        }
        .file-icon-xs::before {
          border-bottom-left-radius: 1px;
          border-width: 3px;
        }
        .file-icon-xs::after {
          content: "";
          border-bottom: 2px solid rgba(255,255,255,.45);
          width: auto;
          left: 2px;
          right: 2px;
              bottom: 3px;
            }
        </style>
    </head>
    <body>
        <table>
            <tr><td><div class="folder"></div></td><td><a href="..">..</a></td></tr>
    """
    directories = []
    files = []
    for f in dirs:
        if f.endswith('index.html'):
            continue
        elif os.path.isdir(f):
            directories.insert(0, "<tr><td><div class=\"folder\"></div></td><td><a href=\"" + f[len(path):] + "\">" + f[len(path):] + "/</a></td></tr>")
            gen_index(f)
        else:
            files.append("<tr><td><div class=\"file-icon file-icon-xs\"></div></td><td><a href=\"" + f[len(path):] + "\">" + f[len(path):-5] + "</a></td></tr>")

    directories.sort()
    files.sort()
    index += "\n".join(directories)
    index += "\n".join(files)
    index += "</table></body></html>"

    with open(path + "/index.html", 'w') as ind:
        ind.write(index)

def mkdir_p(path):
    try:
        os.makedirs(path)
    except OSError as exc: # Python >2.5
        if exc.errno == errno.EEXIST and os.path.isdir(path):
           pass
        else: raise

def safe_open_w(path):
    ''' Open "path" for writing, creating any parent directories as needed.
    '''
    mkdir_p(os.path.dirname(path))
    return open(path, 'w')

theme = 'tango' if len(sys.argv) < 4 else sys.argv[3]

if len(sys.argv) < 2:
    print("No filename specified.")
    exit()
elif len(sys.argv) < 3:
    print("No outdir specified.")
    exit()

files = glob(sys.argv[1] + "/**/*.*", recursive=True)

for f in files:
    if f.endswith(".db"):
        # probably a database. Python doesn't like these.
        continue
    out_name = f[len(sys.argv[1]):]
    try:
        lex = get_lexer_for_filename(f)
    except ClassNotFound:
        try:
            with open(f) as lf:
                lex = guess_lexer(lf.read())
        except ClassNotFound:
            continue
    if lex:
        with open(f) as pf:
            html = highlight(pf.read(), lex, HtmlFormatter(full=True, style=theme, linenos='table'))
            with safe_open_w(sys.argv[2] + out_name + ".html") as wf:
                wf.write(html)

gen_index(sys.argv[2])