<commit_before>#!/usr/bin/python
#
# md2epub.py
#
# by <NAME>
# http://bencrowder.net/
#
# Based off <NAME>'s GetBook.py
# http://staff.washington.edu/mdturner
#

import urllib, re, os, zipfile, glob, shutil, sys, markdown2, string, datetime


class Chapter:
	title = ''
	filename = ''
	htmlfile = ''
	id = ''


class EPub:
	title = ''
	author = ''
	lang = 'en-US'
	basename = ''			# base name of ePub (before the extension)
	path = ''				# directory ePub will be stored in
	bookid = ''				# unique identifier
	url = ''				# URL for this book
	css = ''				# CSS file
	images = []				# list of images to be included
	content = []			# list of Chapters

	def cleanup(self):
		os.rmdir(self.path)

	def save(self):
		# get current working directory
		cwd = os.getcwd()

		# create directory for the ePub
		os.mkdir(self.path)
		os.chdir(self.path)

		try:
			# make mimetype
			f = open('mimetype','w')
			f.write('application/epub+zip')
			f.close()

			# make the META-INF/container.xml
			os.mkdir('META-INF')
			os.chdir('META-INF')
			f = open('container.xml','w')
			f.write('''<?xml version="1.0"?>
	<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
		<rootfiles>
			<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml" />
		</rootfiles>
	</container>''')
			f.close()
			os.chdir('..')

			# make OEBPS/content.opf
			os.mkdir('OEBPS')
			os.chdir('OEBPS')
			f = open('content.opf','w')
			f.write('''<?xml version="1.0"?>
	<package version="2.0" xmlns="http://www.idpf.org/2007/opf"
			 unique-identifier="bookid">
		<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
			<dc:title>''' + self.title + '''</dc:title>
			<dc:creator opf:role="aut">''' + self.author + '''</dc:creator>
			<dc:language>''' + self.lang + '''</dc:language>
			<dc:identifier id="bookid">''' + self.url + ':' + self.bookid + '''</dc:identifier>
		</metadata>
		<manifest>
			<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml" />
	''')

			if self.css:
				f.write('\t\t<item id="style" href="' + os.path.basename(self.css) + '" media-type="application/css" />')

			for chapter in self.content:
				f.write('''
			<item id="''' + chapter.id + '''" href="''' + chapter.htmlfile + '''" media-type="application/xhtml+xml" />''')

			for image in self.images:
				imagefile = os.path.basename(image)
				ext = os.path.splitext(imagefile)[1][1:]	# get the extension
				if ext == 'jpg':
					ext = 'jpeg'
				f.write('''
			<item id="''' + imagefile + '''" href="''' + imagefile + '''" media-type="image/''' + ext + '''" />''')

			f.write('''<commit_msg>Switched to using metadata.opf instead of content.opf<commit_after>#!/usr/bin/python
#
# md2epub.py
#
# by <NAME>
# http://bencrowder.net/
#
# Based off <NAME>'s GetBook.py
# http://staff.washington.edu/mdturner
#

import urllib, re, os, zipfile, glob, shutil, sys, markdown2, string, datetime


class Chapter:
	title = ''
	filename = ''
	htmlfile = ''
	id = ''


class EPub:
	title = ''
	author = ''
	lang = 'en-US'
	basename = ''			# base name of ePub (before the extension)
	path = ''				# directory ePub will be stored in
	bookid = ''				# unique identifier
	url = ''				# URL for this book
	css = ''				# CSS file
	images = []				# list of images to be included
	content = []			# list of Chapters

	def cleanup(self):
		os.rmdir(self.path)

	def save(self):
		# get current working directory
		cwd = os.getcwd()

		# create directory for the ePub
		os.mkdir(self.path)
		os.chdir(self.path)

		try:
			# make mimetype
			f = open('mimetype','w')
			f.write('application/epub+zip')
			f.close()

			# make the META-INF/container.xml
			os.mkdir('META-INF')
			os.chdir('META-INF')
			f = open('container.xml','w')
			f.write('''<?xml version="1.0"?>
	<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
		<rootfiles>
			<rootfile full-path="OEBPS/metadata.opf" media-type="application/oebps-package+xml" />
		</rootfiles>
	</container>''')
			f.close()
			os.chdir('..')

			# make OEBPS/metadata.opf
			os.mkdir('OEBPS')
			os.chdir('OEBPS')
			f = open('metadata.opf','w')
			f.write('''<?xml version="1.0"?>
	<package version="2.0" xmlns="http://www.idpf.org/2007/opf"
			 unique-identifier="bookid">
		<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
			<dc:title>''' + self.title + '''</dc:title>
			<dc:creator opf:role="aut">''' + self.author + '''</dc:creator>
			<dc:language>''' + self.lang + '''</dc:language>
			<dc:identifier id="bookid">''' + self.url + ':' + self.bookid + '''</dc:identifier>
		</metadata>
		<manifest>
			<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml" />
	''')

			if self.css:
				f.write('\t\t<item id="style" href="' + os.path.basename(self.css) + '" media-type="application/css" />')

			for chapter in self.content:
				f.write('''
			<item id="''' + chapter.id + '''" href="''' + chapter.htmlfile + '''" media-type="application/xhtml+xml" />''')

			for image in self.images:
				imagefile = os.path.basename(image)
				ext = os.path.splitext(imagefile)[1][1:]	# get the extension
				if ext == 'jpg':
					ext = 'jpeg'
				f.write('''
			<item id="''' + imagefile + '''" href="''' + imagefile + '''" media-type="image/''' + ext + '''" />''')

			f.write('''
{'fraction_non_alphanumeric': 0.21363211951447245, 'fraction_numerical': 0.008216619981325864, 'mean_word_length': 3.4301075268817205, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 4, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 24}, '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': '1136478', 'n_tokens_mistral': 2314, 'n_tokens_neox': 2029, 'n_words': 393}