Пример #1
0
def make_listing_page(subdir):
	'''Main function to create a directory listing page.'''
	#
	# look if there is a -listing- page
	# preprocess that page
	listing_page_md=get_listing_page(subdir)
	
	if listing_page_md != '':
		infile=os.path.join(config.CONTENT_DIR, subdir, listing_page_md)
		opts=[]
		
		# adding title block extraction here
		# (using them only for the final output below, atm)
		listing_page_body, title_block_vals=extract_title_block(infile, config.REGULAR_TB_LINES)
		
		listing_body_doctype=scan_doctype(listing_page_body)
	
	else:
		title_block_vals=[]
		listing_page_body=''
		listing_body_doctype='strict'
	
	# (debug-print)
	#print('Listing Page Body:', listing_page_body)
	
	# make the content listing
	listing_table_entries_str=gen_listing_table_entries(subdir, listing_page_md)
	
	# (debug-info)
	#print('Listing_table:', listing_table)
	
	# get/set the parent dir (for the path line)
	# this should go in a separate funtion, but it doesn't work
	#getset_parent_dir(subdir)
	# --> now using import config, this could probably be improved ...
	if config.LISTING_PARENT_DIR == '' :
		config.LISTING_PARENT_DIR=subdir
	elif config.LISTING_PARENT_DIR in subdir:
		pass
	else: config.LISTING_PARENT_DIR=subdir
	# (debug-info)
	#print('Subdir:', subdir)
	#print('Parent dir:', LISTING_PARENT_DIR)
	parent_dir=config.LISTING_PARENT_DIR
	
	# generate the path line
	listing_path_items=gen_listing_path_items(subdir, parent_dir, listing_page_md)
	
	# generate the main menu
	main_menu=generate_main_menu(subdir, listing_page_md)
	
	# prepare for output
	final_opts, out_filepath=prepare_final_listing(subdir, listing_path_items, listing_table_entries_str, main_menu, title_block_vals, listing_body_doctype)
	
	# final pandoc processing
	final_html=pandoc_pipe(listing_page_body, final_opts)
	
	# write out
	write_out(final_html, out_filepath)
Пример #2
0
def gallery(subdir, plugin_in):
	gallery_dir=plugin_in
	gallery_name = GALLERY_PREFIX+os.path.basename(gallery_dir)
	
	# check if the directory exists
	if os.path.isdir(plugin_in):
		print("Inserting gallery:", gallery_dir)
		pass
	else:
		dir_not_found_error="GALLERY plugin error: Directory "+gallery_dir+" not found."
		print(dir_not_found_error)
		return dir_not_found_error, dir_not_found_error
	
	## Handle the images:
	# set the out dir
	images_out_dir=os.path.join(config.PUBLISH_DIR, subdir, gallery_name)
	
	# get the images
	filelist=os.listdir(gallery_dir)
	
	image_list=[]
	for file in filelist:
		if file.endswith(".jpg"):
			image_list.append(file)
		elif file.endswith(".png"):
			image_list.append(file)
	
	# make the thumbs and main images
	if not os.path.isdir(images_out_dir):
		os.makedirs(images_out_dir)
	
	thumbs_list=[]
	#main_images_list=[]
	for image in image_list:
		# set the prefix, infile, outfile
		# (make lists for later handling)
		thumb_name=THUMB_IMAGE_PREFIX+image
		thumbs_list.append(thumb_name)
		main_image_name=MAIN_IMAGE_PREFIX+image
		#main_images_list.append(main_image_name)
		
		image_in_path=os.path.join(gallery_dir, image)
		thumb_out_path=os.path.join(images_out_dir, thumb_name)
		main_image_out_path=os.path.join(images_out_dir, main_image_name)
		
		if not os.path.isfile(thumb_out_path):
			convert_image(image_in_path, THUMB_IMAGE_WIDTH, thumb_out_path)
		
		if not os.path.isfile(main_image_out_path):
			convert_image(image_in_path, MAIN_IMAGE_WIDTH, main_image_out_path)
		
		
	
	## Make the HTML:
	# make the thumb lines
	# sort and reverse order for pandoc
	thumbs_list.sort()
	thumbs_list.reverse()
	thumb_lines=[]
	for thumb in thumbs_list:
		img_src=os.path.join(gallery_name, thumb)
		# image alt text should be inserted from db (text file) here
		img_alt=''
		# writing html here to speed up things...
		thumb_line='<img src="{}" alt="{}" onclick="change_image(this, parentNode.parentNode.id)" style="width:auto;"/>'.format(img_src, img_alt)
		
		thumb_lines.append(thumb_line)
	
	# make the gallery
	opts=['--template', GALLERY_TEMPLATE]
	
	opts.append('--variable=gallery-name:'+gallery_name)
	
	for line in thumb_lines:
		opts.append('--variable=thumb-img-line:'+line)
	
	gallery_html=pandoc_pipe('', opts)
	
	# output for PDF production
	if config.PRODUCE_PDF:
		# returning raw content + remark
		#pdf_md = "[[ GALLERY ] [ "+plugin_in+" ]  \n(Gallery plugin, PDF output not yet supported.)\n"
		# (debug-print)
		#print("pdf md: ", pdf_md)
		
		# inserting images
		# (sort list)
		image_list.sort()
		
		# (MD)
		md_text = "Images  \n"
		
		# (adding an MD image reference for every image)
		for image in image_list:
			
			# (make images)
			image_in_path = os.path.join(gallery_dir, image)
			pdf_image_name = PDF_IMAGE_PREFIX+image
			pdf_image_out_path = os.path.join(images_out_dir, pdf_image_name)
			if not os.path.isfile(pdf_image_out_path):
				convert_image(image_in_path, PDF_IMAGE_WIDTH, pdf_image_out_path)
			
			# (we need the absolute path for the PDF production)
			cwd = os.getcwd()
			images_out_dir_abs = os.path.join(cwd, images_out_dir)
			
			img_name = PDF_IMAGE_PREFIX+image
			
			img_src = os.path.join(images_out_dir_abs, img_name)
			img_alt = ''
			
			md_ref = "![ {alt} ]({src})\n".format(alt=img_alt, src=img_src)
			md_text = md_text+md_ref
			
		pdf_md = md_text
		
	else:
		pdf_md = ""
	
	return gallery_html, pdf_md
Пример #3
0
def make_regular_pages(pages_struct, subdir):
	'''Main function to create regular pages.'''
	for page_group in pages_struct:
		# (info-prints)
		print("Page: ", os.path.join(subdir, page_group[0]))
		#print('Preprocessing:', page_group[0])
		#for subcontent in page_group[1:]:
		#	print(' Subcontent:', subcontent)
		#print('Page group:', page_group)
		
		# Preprocess page
		main_page_body_subst, plugin_blocks, plugin_blocks_pdf, main_page_tb_vals=preprocess_page_group(subdir, page_group)
		
		# (scan doctype)
		body_doctype=scan_doctype(main_page_body_subst)
		
		# Preprocess math content
		if config.PROCESS_MATH:
			main_page_body_subst_m = handle_math(subdir, page_group[0], main_page_body_subst)
		else:
			main_page_body_subst_m = main_page_body_subst
		
		# Generate the menus
		#
		# main menu
		# (info-prints)
		#print('Generate main menu.')
		#print('Subdir:', subdir, 'Page group [0]:', page_group[0])
		main_menu=generate_main_menu(subdir, page_group[0])
		
		# section menu
		# (info-prints)
		#print('Generate section menu.')
		section_menu_list=generate_section_menu(subdir, page_group[0])
		
		# Finalize this page
		# prepare for final output
		final_opts, out_filepath=prepare_final(subdir, page_group, main_page_tb_vals, main_menu, section_menu_list, body_doctype)
		
		# (debug-print)
		#print("main page body subst: ", main_page_body_subst)
		
		# final pandoc processing
		final_html_subst=pandoc_pipe(main_page_body_subst_m, final_opts)
		
		# (debug-print)
		#print("final html subst: ", final_html_subst)
		
		# back substitute
		if plugin_blocks != []:
			final_html=back_substitute(final_html_subst, plugin_blocks)
		else:
			final_html=final_html_subst
		
		# write out
		# (info-print)
		print('Writing:', out_filepath)
		write_out(final_html, out_filepath)
		
		# PDF production
		if config.PRODUCE_PDF:
			# (info-print)
			print("Produce a PDF.")
			# (plugins must provide blocks for pdf's)
			generate_pdf(subdir, page_group[0], main_page_body_subst, plugin_blocks_pdf, main_page_tb_vals)
Пример #4
0
def insert_file(subdir, plugin_in):
	# get the content type and path or filename
	fields=plugin_in.split(',')
	type=fields[0]
	
	file_in=fields[-1].strip()
	
	# find out where the file lies
	if not os.path.isabs(file_in):
		filepath=os.path.join(config.CONTENT_DIR, subdir, file_in)
	
	else:
		filepath=file_in
	
	# (debug-info)
	#print('Filepath:', filepath)
	
	# check if it exists
	if os.path.isfile(filepath):
		print("Inserting file:", filepath)
		pass
	else:
		# could make a nicer html error here...
		# and maybe a try/except statement would be better but (?)
		file_not_found_error="INSERTFILE plugin error: File ("+filepath+") not found."
		print(file_not_found_error)
		return file_not_found_error, file_not_found_error
	
	# read the file
	file_op=open(filepath, 'r')
	file_content=file_op.read()
	file_op.close()
	
	# extract additional fields
	add_fields=''
	if len(fields) > 2:
		for field in fields[1:-1]:
			add_fields=' '+field.strip()+add_fields
	
	# append markdown code syntax and type and additional fields
	markdown_code_pre='\n\n~~~ {.'+type+add_fields+' .'+INSERT_FILE_CLASS+'}'
	markdown_code_post='~~~\n\n'
	
	file_content_md=markdown_code_pre+'\n'+file_content+'\n'+markdown_code_post
	
	# process through pandoc
	file_content_html=pandoc_pipe(file_content_md, [])
	
	# insert a title block (table, already html formatted)
	filename=os.path.basename(filepath)
	dir_path=os.path.dirname(file_in)
	if dir_path == '':
		dir_path='.'
	title_line='<table class="InsertFileTitle"><tr><td class="Titlefield">Filename:</td><td class="Textfield" title="Inserted from: '+dir_path+'">'+filename+'</td></tr></table>'
	
	file_content_html_title=title_line+'\n'+file_content_html
	
	# PDF production
	if config.PRODUCE_PDF:
		# create a title
		title_md = "File: "+filename+"\n"
		# (debug)
		#file_content_md = plugin_in
		file_content_md = title_md+file_content_md
		# (debug-print)
		#print("file_content_md: ", file_content_md)
		
	else:
		file_content_md = ""
		# (debug-print)
		#print("md empty...")
	
	return file_content_html_title, file_content_md