def materialy_detail_pdf_static(tutorial_name, chapter_name=None, html_template='pdf_template_default.html'): material_settings = get_tutorial_settings(tutorial_name, chapter_name) if not chapter_name: first_chapter_name = list(material_settings.get('content'))[0] chapter_name = first_chapter_name material_content = material_settings.get('content') md_file_path = str( material_content.get(chapter_name).get("path")).split("/") md_folder_name = str(md_file_path[0]) md_folder_number = "_".join(md_folder_name.split("_")[0:2]) save_directory = os.path.join(os.getcwd(), 'static', 'pdfs', tutorial_name, html_template.split('.')[0]) filename = f"{md_folder_number}-{tutorial_name}-{chapter_name}.pdf" if os.path.exists(os.path.join(save_directory, filename)): return send_from_directory(save_directory, filename, mimetype='application/pdf', as_attachment=False, attachment_filename=filename) else: return "No PDF ..."
def zacni(): material = "zacni" material_settings = get_tutorial_settings(material, None) for chapter_name in material_settings['content']: yield {'kapitola': chapter_name}
def materialy_images(tutorial_name, image, chapter_name=None): material_settings = get_tutorial_settings(tutorial_name, chapter_name) return send_from_directory(os.path.join( 'materialy', tutorial_name, material_settings['chapter_folder'], 'images'), image, mimetype='image/gif')
def materialy_detail_all_pdfs(tutorial_name, html_template='pdf_template_default.html'): material_settings = get_tutorial_settings(tutorial_name, None) for _chapter in material_settings.get('content'): materialy_detail_pdf(tutorial_name, chapter_name=_chapter, html_template=html_template)
def materialy_detail(): material_dirs = os.listdir('materialy') for material in material_dirs: print(material) yield {'tutorial_name': material} material_settings = get_tutorial_settings(material, None) for chapter_name in material_settings['content']: yield { 'tutorial_name': material, 'chapter_name': chapter_name, 'html_template': 'materialy_detail.html' }
def materialy_images(): material_dirs = os.listdir('materialy') for material in material_dirs: material_settings = get_tutorial_settings(material, None) for _chapter_name in material_settings['content']: _chapter_folder = material_settings['content'][_chapter_name][ 'path'].split("/")[0] _img_folder_path = os.path.join('materialy', material, _chapter_folder, 'images') # print(_chapter_name) if os.path.exists(_img_folder_path): _images = os.listdir(_img_folder_path) for _image in _images: yield { "tutorial_name": material, "image": _image, 'chapter_name': _chapter_name }
def materialy_detail_pdf(tutorial_name, chapter_name=None, html_template='pdf_template_default.html'): material_settings = get_tutorial_settings(tutorial_name, chapter_name) if not chapter_name: first_chapter_name = list(material_settings.get('content'))[0] chapter_name = first_chapter_name material_content = material_settings.get('content') md_file_path = str( material_content.get(chapter_name).get("path")).split("/") md_folder_name = str(md_file_path[0]) md_folder_number = "_".join(md_folder_name.split("_")[0:2]) appState = { "recentDestinations": [{ "id": "Save as PDF", "origin": "local", "account": "", }], "selectedDestinationId": "Save as PDF", "version": 2, "isHeaderFooterEnabled": False, "isCssBackgroundEnabled": True, } save_directory = os.path.join(os.getcwd(), 'static', 'pdfs', tutorial_name, html_template.split('.')[0]) filename = f"{md_folder_number}-{tutorial_name}-{chapter_name}.pdf" filename_teacher = f"{md_folder_number}-{tutorial_name}-{chapter_name}-teacher.pdf" if not os.path.exists(save_directory): os.makedirs(save_directory) if os.path.exists(os.path.join(save_directory, filename)): os.remove(os.path.join(save_directory, filename)) if os.path.exists(os.path.join(save_directory, filename_teacher)): os.remove(os.path.join(save_directory, filename_teacher)) profile = { 'printing.print_preview_sticky_settings.appState': json.dumps(appState), 'savefile.default_directory': save_directory } chrome_options = webdriver.ChromeOptions() chrome_options.add_experimental_option('prefs', profile) chrome_options.add_argument('--kiosk-printing') # chrome_options.add_argument("--headless") if os.path.exists("chromedriver.exe"): chromedriver_path = os.path.abspath("chromedriver.exe") elif os.path.exists("chromedriver"): chromedriver_path = os.path.abspath("chromedriver") else: return "Please put 'chromedriver' or 'chromedriver.exe' into the project directory." driver = webdriver.Chrome(options=chrome_options, executable_path=chromedriver_path) driver.get( url_for('materialy_detail_print', metodika=tutorial_name, kapitola=chapter_name, html_template=html_template, _external=True)) driver.execute_script( f"var tempTitle = document.title;document.title = '{filename}';window.print();document.title = tempTitle;" ) while not os.path.exists(os.path.join(save_directory, filename)): sleep(1) driver.quit() return send_from_directory(save_directory, filename, mimetype='application/pdf', as_attachment=False, attachment_filename=filename)
def materialy_detail(tutorial_name, chapter_name=None, html_template='materialy_detail.html', material_base_url="materialy/"): material_settings = get_tutorial_settings(tutorial_name, chapter_name) if not chapter_name: chapter_name = material_settings.get('chapter_name') material_content = material_settings.get('content') md_file_path = os.path.split( str(material_content.get(chapter_name).get("path"))) content_path = os.path.join('materialy', tutorial_name, *md_file_path) with codecs.open(content_path, mode="r", encoding="utf-8") as file: content_raw = file.read() for _chapter in material_settings.get('content'): _md_file_path = str( material_content.get(_chapter).get("path")).split("/") _content_path = os.path.join('materialy', tutorial_name, *_md_file_path) with codecs.open(_content_path, mode="r", encoding="utf-8") as file: _content_raw = file.read() meta_title = markdown_meta(_content_raw).get('title') if meta_title: material_settings.get('content').get( _chapter)['title'] = meta_title[0] meta_sub_title = markdown_meta(_content_raw).get('subtitle') if meta_sub_title: material_settings.get('content').get( _chapter)['subtitle'] = meta_sub_title[0] content_html = markdown_to_html( content_raw, img_url=f"/materialy/{tutorial_name}/{chapter_name}/", html_template=html_template) # Retrieve chapter title chapter_title = material_settings.get('content').get(chapter_name).get( 'title') chapter_subtitle = material_settings.get('content').get(chapter_name).get( 'subtitle') # Retrieve Video Tab video_content_html = None video_content_path = os.path.join('materialy', tutorial_name, md_file_path[0], "video.md") if os.path.exists(video_content_path): with codecs.open(video_content_path, mode="r", encoding="utf-8") as file: video_content_raw = file.read() video_content_html = markdown_to_html( video_content_raw, img_url=f"/materialy/{tutorial_name}/{chapter_name}/", html_template=html_template) # Retrieve Hidden Video Tab hidden_video_content = False hidden_video_content_path = os.path.join('materialy', tutorial_name, md_file_path[0], "_video.md") if os.path.exists(hidden_video_content_path): with codecs.open(hidden_video_content_path, mode="r", encoding="utf-8") as file: hidden_video_content_raw = file.read() video_content_html = markdown_to_html( hidden_video_content_raw, img_url=f"/materialy/{tutorial_name}/{chapter_name}/", html_template=html_template) hidden_video_content = True # Retrieve Teacher Tab teacher_content_html = None teacher_content_path = os.path.join('materialy', tutorial_name, md_file_path[0], "teacher.md") if os.path.exists(teacher_content_path): with codecs.open(teacher_content_path, mode="r", encoding="utf-8") as file: teacher_content_raw = file.read() teacher_content_html = markdown_to_html( teacher_content_raw, img_url=f"/materialy/{tutorial_name}/{chapter_name}/", html_template=html_template) # Retrieve Hidden Teacher Tab hidden_teacher_content = False hidden_teacher_content_path = os.path.join('materialy', tutorial_name, md_file_path[0], "_teacher.md") if os.path.exists(hidden_teacher_content_path): with codecs.open(hidden_teacher_content_path, mode="r", encoding="utf-8") as file: hidden_teacher_content_raw = file.read() teacher_content_html = markdown_to_html( hidden_teacher_content_raw, img_url=f"/materialy/{tutorial_name}/{chapter_name}/", html_template=html_template) hidden_teacher_content = True material_settings['downloads'] = {} ## Donwloads Tab material_content = material_settings.get('content') md_file_path = str( material_content.get(chapter_name).get("path")).split("/") md_folder_name = str(md_file_path[0]) md_folder_number = "_".join(md_folder_name.split("_")[0:2]) if os.path.exists( os.path.join( os.getcwd(), 'static', 'pdfs', tutorial_name, "pdf_template_default", f"{md_folder_number}-{tutorial_name}-{chapter_name}.pdf")): material_settings['downloads']["StiahnuƄ Lekciu ako PDF"] = url_for( 'static', filename= f"pdfs/{tutorial_name}/pdf_template_default/{md_folder_number}-{tutorial_name}-{chapter_name}.pdf" ) template_variables = _get_template_variables( chapter_title=chapter_title, chapter_subtitle=chapter_subtitle, material_slug=tutorial_name, chapter_slug=chapter_name, settings=material_settings, content_html=content_html, material_settings=material_settings, material_base_url=material_base_url, chapter_id=material_settings['chapter_id'], teacher_content_html=teacher_content_html, hidden_teacher_content=hidden_teacher_content, video_content_html=video_content_html, hidden_video_content=hidden_video_content) return render_template(html_template, **template_variables)