<commit_before>#!/usr/bin/env python

""" Create simple gallery pages for each directory in content_dir/images/
"""

import sys, os.path
import commands # rather dirty but quick for these scripts
from argparse import ArgumentParser

_DEST = "pages/galleries/"

GALLERY_START = '\n.. raw:: html\n\n    <div class="gallery">\n\n'
GALLERY_END = '\n.. raw:: html\n\n    </div>\n\n'

PAGE_SOURCES = "../fret/galleries/"

PROG = ''

def get_content_dir():
    parser = ArgumentParser(description=__doc__)
    parser.add_argument(dest='content_dir')
    args = parser.parse_args()
    return os.path.abspath(args.content_dir)

def get_galleries(content_dir):
    image_root = os.path.join(content_dir, 'images')
    cmd = "find %s -type d" % image_root
    image_dirs = commands.getoutput(cmd).split('\n')
    galleries = {} # name: image_dir, ...
    for img_dir in image_dirs:
        name = img_dir.replace(image_root, '')
        name.replace('/', ' - ')
        if name == '':
            name = "Images"
        if name[0] == '/':
            name = name[1:]
        galleries[name] = img_dir
    return galleries

def get_image_directive(src, alt_text):
    return """
.. image:: %s
    :alt: %s
    :width: 300
    :align: center
""" % (src, alt_text)

def get_title(title_name):
    val = '=' * len(title_name)
    val += '\n%s\n' % title_name
    val += '=' * len(title_name)
    return val

def get_page(fname, title):
    if os.path.exists(fname):
        with open(fname, 'r') as fin:
            return fin.read()
    else:
        return get_title(title) + "\n\n:summary:\n"

def write_gallery(write, name, content_dir, img_dir):
    title_name = name.replace('/', ' - ')
    _name = name.replace('/', '-')
    url = os.path.join(_DEST, "%s.html" % _name)
    fname = os.path.join(PAGE_SOURCES, "%s.rst" % _name)

    write(get_page(fname, title_name))
    write('\n:save_as: ' + url + '\n' + GALLERY_START + '\n')
    imgs = commands.getoutput('find %s -iname "*.jpg" -o -iname "*.png"' %
                              img_dir).split('\n')
    imgs.sort()
    for img in imgs:
        _, alt_text = os.path.split(img)
        img = img.replace(content_dir, '../..')
        write('\n%s' % get_image_directive(img, alt_text))
    write('\n' + GALLERY_END)

def main():
    global PROG
    _, PROG = os.path.split(sys.argv[0])
    print "%s: building galleries" % PROG
    content_dir = get_content_dir()
    for name, img_dir in get_galleries(content_dir).iteritems():<commit_msg>Turned galleries into normal posts and added default dates to them<commit_after>#!/usr/bin/env python

""" Create simple gallery pages for each directory in content_dir/images/
"""

import sys, os.path
import commands # rather dirty but quick for these scripts
from argparse import ArgumentParser

_DEST = "galleries/"

GALLERY_START = '\n.. raw:: html\n\n    <div class="gallery">\n\n'
GALLERY_END = '\n.. raw:: html\n\n    </div>\n\n'

PAGE_SOURCES = "../fret/galleries/"

PROG = ''

def get_content_dir():
    parser = ArgumentParser(description=__doc__)
    parser.add_argument(dest='content_dir')
    args = parser.parse_args()
    return os.path.abspath(args.content_dir)

def get_galleries(content_dir):
    image_root = os.path.join(content_dir, 'images')
    cmd = "find %s -type d" % image_root
    image_dirs = commands.getoutput(cmd).split('\n')
    galleries = {} # name: image_dir, ...
    for img_dir in image_dirs:
        name = img_dir.replace(image_root, '')
        name.replace('/', ' - ')
        if name == '':
            name = "Images"
        if name[0] == '/':
            name = name[1:]
        galleries[name] = img_dir
    return galleries

def get_image_directive(src, alt_text):
    return """
.. image:: %s
    :alt: %s
    :width: 300
    :align: center
""" % (src, alt_text)

def get_title(title_name):
    val = '=' * len(title_name)
    val += '\n%s\n' % title_name
    val += '=' * len(title_name)
    return val

def get_page(fname, title):
    if os.path.exists(fname):
        with open(fname, 'r') as fin:
            return fin.read()
    else:
        return (get_title(title) +
                "\n\n:summary:\n:date: 1900-01-01 01:01\n\n")

def write_gallery(write, name, content_dir, img_dir):
    title_name = name.replace('/', ' - ')
    _name = name.replace('/', '-')
    url = os.path.join(_DEST, "%s.html" % _name)
    fname = os.path.join(PAGE_SOURCES, "%s.rst" % _name)

    write(get_page(fname, title_name))
    write('\n:save_as: ' + url + '\n' + GALLERY_START + '\n')
    imgs = commands.getoutput('find %s -iname "*.jpg" -o -iname "*.png"' %
                              img_dir).split('\n')
    imgs.sort()
    for img in imgs:
        _, alt_text = os.path.split(img)
        img = img.replace(content_dir, '../..')
        write('\n%s' % get_image_directive(img, alt_text))
    write('\n' + GALLERY_END)

def main():
    global PROG
    _, PROG = os.path.split(sys.argv[0])
    print "%s: building galleries" % PROG
    content_dir = get_content_dir()
    for name, img_dir in get_galleries(content_dir).iteritems():
{'fraction_non_alphanumeric': 0.16298342541436464, 'fraction_numerical': 0.004735595895816891, 'mean_word_length': 2.727205882352941, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2187398', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1797, 'n_words': 397}