def get(self, request, *args, **kwargs): contest_slug = kwargs['contest'] problem_letter = kwargs['problem'] problem_text = '' contest = get_object_or_404(Contest, active=True, start__lte=now(), slug=contest_slug) user = request.user.member first_name = request.user.first_name if not entry_exists(user, contest): HttpRedirect('/contest-gateway') problem = get_object_or_404(Problem, letter=problem_letter) if problem not in contest.problems.all(): raise Http404 try: buff = StringIO() index_file = 'grader/contests/' + contest_slug + '/' + problem_letter + '.md' markdownFromFile(input=index_file, output=buff) problem_text = buff.getvalue() except IOError: raise Http404 return render(request, self.template, {'problem':problem, 'problem_text':problem_text, 'first_name':first_name,})
def MakeSourceDist( ): distDir = "imfit-%s/" % VERSION_STRING final_file_list = example_file_list + misc_required_files_list + documentation_file_list final_file_list += extras_file_list final_file_list += funcobj_file_list final_file_list += solvers_file_list final_file_list += mcmc_file_list final_file_list += core_file_list final_file_list += python_file_list final_file_list += testing_scripts_list final_file_list += test_file_imfit_list final_file_list += test_file_mcmc_list final_file_list += test_file_makeimage_list final_file_list += test_file_list final_file_list.append("SConstruct") tar = tarfile.open(SOURCE_TARFILE, 'w|gz') for fname in final_file_list: tar.add(distDir + fname) tar.close() print("Copying gzipped tar file %s to %s..." % (SOURCE_TARFILE, SOURCE_COPY_DEST_DIR)) shutil.copy(SOURCE_TARFILE, SOURCE_COPY_DEST_DIR) print("Generating HTML version of CHANGELOG.md and copying to %s..." % (MAC_CHANGELOG_DEST)) markdown.markdownFromFile(input=MAC_CHANGELOG_MD, output=MAC_CHANGELOG_DEST)
def convert(): '''Convert MD to HTML from MD file.''' markdownFromFile(input='example_readme.md', output='example_readme.html', encoding='utf8', extensions=['fenced_code']) wb_open('http://localhost:8000/example_readme.html')
def preview(recieved_file, user_id, return_url=True): user_id = str(user_id) media_temp_location = settings.MEDIA_TEMP current_time = str(round(time.time())) preview_filename = str(user_id + '-' + current_time + '.md') preview_output_filename = str(user_id + '-' + current_time + '.html') # Saves the recieved file. fs = FileSystemStorage(location=media_temp_location) fs.save(preview_filename, recieved_file) # Absolute paths for the input and output files. input_file = os.path.join(media_temp_location, preview_filename) output_file = os.path.join(media_temp_location, preview_output_filename) # Converts the markdown file to HTML output file. markdown.markdownFromFile(input=input_file, output=output_file, encoding='utf-8') # Creates the post_id. post_id = str(user_id + '-' + current_time) if return_url is True: new_url = settings.MEDIA_TEMP_URL + preview_output_filename return new_url, post_id else: return post_id
def load_legal_doc(doc_name, locale): """ Return the HTML content of a legal doc in the requested locale. :param doc_name: name of the legal doc folder :param locale: preferred language version of the doc :return: string content of the file or None """ source = path.join(LEGAL_DOCS_PATH, doc_name, locale + '.md') output = StringIO.StringIO() if not path.exists(source): source = path.join(LEGAL_DOCS_PATH, doc_name, 'en-US.md') try: # Parse the Markdown file md.markdownFromFile( input=source, output=output, extensions=['attr_list', 'headerid', 'outline(wrapper_cls=)']) content = output.getvalue().decode('utf8') except IOError: return None finally: output.close() return content
def MakeSourceDist(): distDir = "imfit-%s/" % VERSION_STRING final_file_list = example_file_list + misc_required_files_list + documentation_file_list final_file_list += extras_file_list final_file_list += funcobj_file_list final_file_list += solvers_file_list final_file_list += mcmc_file_list final_file_list += core_file_list final_file_list += python_file_list final_file_list += testing_scripts_list final_file_list += test_file_imfit_list final_file_list += test_file_mcmc_list final_file_list += test_file_makeimage_list final_file_list += test_file_list final_file_list.append("SConstruct") tar = tarfile.open(SOURCE_TARFILE, 'w|gz') for fname in final_file_list: tar.add(distDir + fname) tar.close() print("Copying gzipped tar file %s to %s..." % (SOURCE_TARFILE, SOURCE_COPY_DEST_DIR)) shutil.copy(SOURCE_TARFILE, SOURCE_COPY_DEST_DIR) print("Generating HTML version of CHANGELOG.md and copying to %s..." % (MAC_CHANGELOG_DEST)) markdown.markdownFromFile(input=MAC_CHANGELOG_MD, output=MAC_CHANGELOG_DEST)
def run(self): print("RUNNING md_to_html_build_py") if not self.dry_run: for directory, files in self.files.items(): target_dir = os.path.join(self.build_lib, directory) self.mkpath(target_dir) for entry in files: if isinstance(entry, tuple): if len(entry) != 2: continue source, dest = entry[0], os.path.join( target_dir, entry[1]) else: source = entry dest = os.path.join(target_dir, source + ".html") print("Rendering markdown from {} to {}".format( source, dest)) from markdown import markdownFromFile markdownFromFile(input=source, output=dest, encoding="utf-8") baseclass.run(self)
def read_page(filename): page = dict() page['id'] = os.path.splitext(os.path.basename(filename))[0] string_file = StringIO.StringIO() markdown.markdownFromFile(input=filename, output=string_file) page['content'] = string_file.getvalue() return page
def run(): options, logging_level = parse_options() if not options: sys.exit(2) logger.setLevel(logging_level) logger.addHandler(logging.StreamHandler()) markdown.markdownFromFile(**options)
def validate_status(status, output_dir, working_dir, default_image): # validate that status readme file exists status_readme_file = status['id'] + '.md' if not os.path.exists(os.path.join(working_dir, status_readme_file)): error('Status readme file not found: ' + status_readme_file) # render markdown html from_markdown = os.path.join(working_dir, status_readme_file) to_html = os.path.join(output_dir, os.path.splitext(status_readme_file)[0] + '.html') markdown.markdownFromFile( input=from_markdown, output=to_html, encoding='utf8', ) # validate that image exists image = status['id'] + '.png' if not os.path.exists(os.path.join(working_dir, image)): error('image not found: ' + image) # make sure all images within the object have the same size status_image = PIL.Image.open(os.path.join(working_dir, image)) img_width, img_height = status_image.size if status_image.size != default_image.size: print('status image validation failed: ' + status['name']) print('status_image.size = ' + str(status_image.size)) print('default_image.size = ' + str(default_image.size)) error('Different image sizes wihin the same object') validate_mapping(status['mapping'])
def markdown_to_html(fName_input): print("Rendering markdown text from {0}".format(fName_input)) fName_output = "../docs/{0}.html".format(os.path.splitext(fName_input)[0]) markdown.markdownFromFile(input=("../" + fName_input), output=fName_output)
def get_page(request, file_name): autorized_files_list = [] for i in os.listdir(os.path.join(settings.BASE_DIR, "git_content")): if os.path.isdir(i): continue if not i.endswith(".md"): continue autorized_files_list.append(i.rstrip(".md")) if file_name in ("", "/"): file_name = "index" if file_name not in autorized_files_list: raise Http404 # html = markdown_path(os.path.join(settings.BASE_DIR, "git_content", file_name + ".md")) try: # Python2 from StringIO import StringIO html = StringIO() except ImportError: # Python3 from io import BytesIO html = BytesIO() markdownFromFile(input=os.path.join(settings.BASE_DIR, "git_content", file_name + ".md"), output=html) html.read() return render(request, "index.html", { "content": html.getvalue(), })
def run(): options, logging_level = parse_options() if not options: sys.exit(2) logger.setLevel(logging_level) logger.addHandler(logging.StreamHandler()) markdown.markdownFromFile(**options)
def plugin_markdown(parameters=[]): result = [] for parameter in parameters: filename = parameter + '.html' markdown.markdownFromFile(parameter, filename) result.append(filename) return result
def render_markdown_from_file(f, **markdown_kwargs): """Render Markdown text from a file stream to HTML.""" s = StringIO() markdownFromFile(input=f, output=s, **markdown_kwargs) html = s.getvalue() s.close() return html
def _render(self): buffer = StringIO() self.obj.file.open() markdown.markdownFromFile(input=self.obj.file, output=buffer, output_format="xhtml1", safe_mode="escape") rendered = buffer.getvalue() buffer.close() return rendered
def section_to_html(section): """ Convert a markdown file in the markdown folder and convert it into a template snippet in the templates folder """ md_fil_name = 'markdown/{}.md'.format(section) html_fil_name = 'templates/{}.html'.format(section) markdown.markdownFromFile(input=md_fil_name, output=html_fil_name)
def run(): """Run Markdown from the command line.""" options, logging_level = parse_options() if not options: sys.exit(2) logger.setLevel(logging_level) logger.addHandler(logging.StreamHandler()) markdown.markdownFromFile(**options)
def process_md(md, h_str, f_str, indir, outdir): outfilename = os.path.join(outdir, md[0:-3] + ".html") infilename = os.path.join(indir, md) with open(outfilename, "w") as f: f.write(h_str) markdownFromFile(output_format="xhtml1",input=infilename, output=f) f.write(f_str)
def render_markdown_from_file(f, **markdown_kwargs): """Render Markdown text from a file stream to HTML.""" s = StringIO() markdownFromFile(input=f, output=s, **markdown_kwargs) html = s.getvalue() s.close() return html
def get_post_content(dir, post_time, post_name): file_name = os.path.join(dir, post_time + '-' + post_name + '.md') if not os.path.exists(file_name): return None output = StringIO.StringIO() markdown.markdownFromFile(input = file_name, output = output, extensions = ['markdown.extensions.tables']) content = output.getvalue() output.close() return content
def main(): # Available output formats are: # - "xhtml1": Outputs XHTML 1.x. Default. # - "xhtml5": Outputs XHTML style tags of HTML 5 # - "xhtml": Outputs latest supported version of XHTML (currently XHTML 1.1). # - "html4": Outputs HTML 4 # - "html5": Outputs HTML style tags of HTML 5 # - "html": Outputs latest supported version of HTML (currently HTML 4). markdown.markdownFromFile(input="test.md", output="test.html", output_format="html5")
def load_legal_doc(doc_name, locale): """ Return the HTML content of a legal doc in the requested locale. :param doc_name: name of the legal doc folder :param locale: preferred language version of the doc :return: dict containing string content of the file (or None), a boolean value indicating whether the file is localized into the specified locale, and a dict of all available locales for that document """ # for file access convert bedrock locale to legal-docs equivalent if locale in BEDROCK_LOCALES_TO_LEGAL_DOCS: locale = BEDROCK_LOCALES_TO_LEGAL_DOCS[locale] source_dir = path.join(LEGAL_DOCS_PATH, doc_name) source_file = path.join(source_dir, locale + '.md') output = StringIO.StringIO() locales = [ f.replace('.md', '') for f in listdir(source_dir) if f.endswith('.md') ] # convert legal-docs locales to bedrock equivalents locales = [LEGAL_DOCS_LOCALES_TO_BEDROCK.get(l, l) for l in locales] # filter out non-production locales and convert to dict with names translations = get_translations_native_names(locales) localized = locale != settings.LANGUAGE_CODE # it's possible the legal-docs repo changed the filename to match our locale. # this makes it work for mapped locales even if the map becomes superfluous. if not path.exists( source_file) and locale in LEGAL_DOCS_LOCALES_TO_BEDROCK: locale = LEGAL_DOCS_LOCALES_TO_BEDROCK[locale] source_file = path.join(source_dir, locale + '.md') if not path.exists(source_file): source_file = path.join(LEGAL_DOCS_PATH, doc_name, 'en-US.md') localized = False try: # Parse the Markdown file md.markdownFromFile(input=source_file, output=output, extensions=[ 'attr_list', 'headerid', OutlineExtension((('wrapper_cls', ''), )) ]) content = output.getvalue().decode('utf8') except IOError: content = None localized = False finally: output.close() return { 'content': content, 'localized': localized, 'translations': translations, }
def load_legal_doc(request, doc_name): """ Load a static Markdown file and return the document as a BeautifulSoup object for easier manipulation. """ locale = l10n_utils.get_locale(request) source = path.join(LEGAL_DOCS_PATH, doc_name, locale + '.md') output = StringIO.StringIO() if not path.exists(source): source = path.join(LEGAL_DOCS_PATH, doc_name, 'en-US.md') # Parse the Markdown file md.markdownFromFile(input=source, output=output, extensions=['attr_list', 'outline(wrapper_cls=)']) content = output.getvalue().decode('utf8') output.close() soup = BeautifulSoup(content) hn_pattern = re.compile(r'^h(\d)$') href_pattern = re.compile(r'^https?\:\/\/www\.mozilla\.org') # Manipulate the markup for section in soup.find_all('section'): level = 0 header = soup.new_tag('header') div = soup.new_tag('div') section.insert(0, header) section.insert(1, div) # Append elements to <header> or <div> for tag in section.children: match = hn_pattern.match(tag.name) if match: header.append(tag) level = int(match.group(1)) if tag.name == 'p': (header if level == 1 else div).append(tag) if tag.name in ['ul', 'hr']: div.append(tag) if level > 3: section.parent.div.append(section) # Remove empty <div>s if len(div.contents) == 0: div.extract() # Convert the site's full URLs to absolute paths for link in soup.find_all(href=href_pattern): link['href'] = href_pattern.sub('', link['href']) # Return the HTML flagment as a BeautifulSoup object return soup
def run(): """Run Markdown from the command line.""" # Parse options and adjust logging level if necessary options, logging_level = parse_options() if not options: sys.exit(0) if logging_level: logging.getLogger('MARKDOWN').setLevel(logging_level) # Run markdown.markdownFromFile(**options)
def load_legal_doc(request, doc_name): """ Load a static Markdown file and return the document as a BeautifulSoup object for easier manipulation. """ locale = l10n_utils.get_locale(request) source = path.join(LEGAL_DOCS_PATH, doc_name, locale + '.md') output = StringIO.StringIO() if not path.exists(source): source = path.join(LEGAL_DOCS_PATH, doc_name, 'en-US.md') # Parse the Markdown file md.markdownFromFile(input=source, output=output, extensions=['attr_list', 'outline(wrapper_cls=)']) content = output.getvalue().decode('utf8') output.close() soup = BeautifulSoup(content) hn_pattern = re.compile(r'^h(\d)$') href_pattern = re.compile(r'^https?\:\/\/www\.mozilla\.org') # Manipulate the markup for section in soup.find_all('section'): level = 0 header = soup.new_tag('header') div = soup.new_tag('div') section.insert(0, header) section.insert(1, div) # Append elements to <header> or <div> for tag in section.children: match = hn_pattern.match(tag.name) if match: header.append(tag) level = int(match.group(1)) if tag.name == 'p': (header if level == 1 else div).append(tag) if tag.name in ['ul', 'hr']: div.append(tag) if level > 3: section.parent.div.append(section) # Remove empty <div>s if len(div.contents) == 0: div.extract() # Convert the site's full URLs to absolute paths for link in soup.find_all(href=href_pattern): link['href'] = href_pattern.sub('', link['href']) # Return the HTML flagment as a BeautifulSoup object return soup
def main(): # Available output formats are: # - "xhtml1": Outputs XHTML 1.x. Default. # - "xhtml5": Outputs XHTML style tags of HTML 5 # - "xhtml": Outputs latest supported version of XHTML (currently XHTML 1.1). # - "html4": Outputs HTML 4 # - "html5": Outputs HTML style tags of HTML 5 # - "html": Outputs latest supported version of HTML (currently HTML 4). markdown.markdownFromFile(input="test.md", output="test.html", output_format="html5")
def __convertReadMeToHtml(outDir, readMeSrcPath): outFilename = readMeSrcPath.with_suffix(".html").name readMeOutPath = outDir / "doc" / "en" / outFilename readMeOutPath.parent.mkdir(parents=True) readMeOutPath.touch() markdown.markdownFromFile(input=str(readMeSrcPath), output=str(readMeOutPath)) return outFilename
def _render(self): buffer = StringIO() self.obj.file.open() markdown.markdownFromFile(input=self.obj.file, output=buffer, output_format='xhtml1', safe_mode='escape') rendered = buffer.getvalue() buffer.close() return rendered
def run(): #pragma: no cover """Run Markdown from the command line.""" # Parse options and adjust logging level if necessary options, logging_level = parse_options() if not options: sys.exit(2) logger.setLevel(logging_level) logger.addHandler(logging.StreamHandler()) # Run markdown.markdownFromFile(**options)
def run(): """Run Markdown from the command line.""" # Parse options and adjust logging level if necessary options, logging_level = parse_options() if not options: sys.exit(2) logger.setLevel(logging_level) logger.addHandler(logging.StreamHandler()) # Run markdown.markdownFromFile(**options)
def readme(): import markdown, tempfile, os text = open("README.md", encoding='utf-8') # md=markdown.markdown(text, safe_mode=False) stream = tempfile.TemporaryFile() markdown.markdownFromFile(input="README.md", output=stream) stream.seek(os.SEEK_SET) text = stream.read().decode() # print(text) return """ <html> <head> <title>README | By SpeedX</title> </head> <body>""" + str( text) + """</body></html>"""
def main(): base = os.path.abspath(os.path.dirname(__file__)) index = os.path.join(base, "index.html.tmpl") readme = os.path.join(os.path.dirname(base), "README.md") templ = open(index).read() buf = StringIO.StringIO("rw") markdown.markdownFromFile(input=readme, output=buf) print templ.format(body=buf.getvalue())
def main(): base = os.path.abspath(os.path.dirname(__file__)) index = os.path.join(base, "index.html.tmpl") readme = os.path.join(os.path.dirname(base), "README.md") templ = open(index).read() buf = StringIO.StringIO("rw") markdown.markdownFromFile(input=readme, output=buf) print templ.format(body=buf.getvalue())
def parse_markdown(f, root): """ This function parses a markdown file. Args: f = file name root = root folder for the given file """ out = os.path.join(root, f[:f.rindex('.')] + '.html') inp = os.path.join(root, f) print 'parsing %s' % (f) markdown.markdownFromFile(input=inp, output=out)
def process_markdown_files(): for markdown_file in os.listdir(input_dxtx_path): print("Markdown file = " + markdown_file) if markdown_file.endswith(".txt"): html_file = os.path.splitext(markdown_file)[0] + '.html' html_file_path = os.path.join(output_dxtx_path, html_file) markdown_file_path = os.path.join(input_dxtx_path, markdown_file) print("Parsing markdown file " + markdown_file_path + " and saving as as html file " + html_file_path) # use footnote extensions markdown.markdownFromFile(input=markdown_file_path, output=html_file_path, extensions=['markdown.extensions.footnotes', 'markdown.extensions.tables'])
def on_modified(self, event): markdown.markdownFromFile( input=os.path.join(args.path, INPUT), output=os.path.join(args.path, OUTPUT), encoding="iso-8859-1", extensions=[ 'markdown.extensions.tables', 'markdown.extensions.sane_lists', 'markdown.extensions.wikilinks', MarkdownInclude(configs={'base_path': args.path}), TocExtension(anchorlink=True), ])
def render_markdown_from_file(f): """Renders Markdown text to HTML. The Markdown text will be sanitized to prevent injecting custom HTML. It will also enable a few plugins for code highlighting and sane lists. """ s = StringIO() markdownFromFile(input=f, output=s, **MARKDOWN_KWARGS) html = s.getvalue() s.close() return html
def render_markdown_from_file(f): """Renders Markdown text to HTML. The Markdown text will be sanitized to prevent injecting custom HTML. It will also enable a few plugins for code highlighting and sane lists. """ s = StringIO() markdownFromFile(input=f, output=s, **MARKDOWN_KWARGS) html = s.getvalue() s.close() return html
def generate(file, config): """ Write expected output file for given input. """ cfg_section = get_section(file, config) if config.get(cfg_section, "skip") or config.get(cfg_section, "normalize"): print("Skipping:", file) return None input_file = file + config.get(cfg_section, "input_ext") output_file = file + config.get(cfg_section, "output_ext") if not os.path.isfile(output_file) or os.path.getmtime(output_file) < os.path.getmtime(input_file): print("Generating:", file) markdown.markdownFromFile(input=input_file, output=output_file, encoding="utf-8", **get_args(file, config)) else: print("Already up-to-date:", file)
def text2markdown( self, text ): """( BasicRender, str ) -> str transforms input sring into basic markdown using "markdown" lib from standard library. Be careful returned value is generally multiple so needs to be corectly escaped if sending to javascript. """ crude_input = StringIO.StringIO( text ) out_obj = StringIO.StringIO( ) markdown.markdownFromFile( input = crude_input, output = out_obj ) markdowntext = out_obj.getvalue() return markdowntext
def do_GET(self): basename = os.path.basename(self.path) if not basename.endswith('.md'): SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) else: fh = self.send_head() for s in self.server.stylesheets: self.wfile.write(r'<link rel="stylesheet" href="{}" type="text/css" />'.format(s)) markdown.markdownFromFile( os.path.join('.', self.path[1:]), self.wfile, extensions=['codehilite', 'toc(title=Table of Contents)']) fh.close()
def get(self, request, *args, **kwargs): contest_slug = kwargs['contest'] contest = get_object_or_404(Contest, active=True, start__lte=now(), slug=contest_slug) user = request.user.member first_name = request.user.first_name problems = contest.problems.all() contest_text = '' try: buff = StringIO() index_file = 'grader/contests/' + contest_slug + '/index.md' markdownFromFile(input=index_file, output=buff) contest_text = buff.getvalue() except IOError: raise Http404 formatted_problems = [] for p in problems: try: buff = StringIO() index_file = 'grader/contests/' + contest_slug + '/' + p.letter + '.md' markdownFromFile(input=index_file, output=buff) problem_text = buff.getvalue() except IOError: problem_text = '' formatted_problems.append((p.letter, problem_text,)) if entry_exists(user, contest): return render(request, self.template, {'contest': contest, 'problems':formatted_problems, 'contest_text':contest_text, 'first_name': first_name,}) entry = Entry(member=user, contest=contest) entry.save() for p in problems: ps = ProblemScore() ps.letter = p.letter ps.entry = entry ps.save() subtasks = p.subtasks.all() for st in subtasks: sts = SubtaskScore() sts.num = st.num sts.problem_score = ps sts.save() return render(request, self.template, {'contest':contest, 'problems':formatted_problems, 'contest_text':contest_text, 'first_name': first_name,})
def ci_build(pull_request_id, patch_url): r = requests.get( "https://api.github.com/repos/{}/{}/pulls/{}/files".format( config.githubUsername, config.githubRepo, pull_request_id), headers=githubHeader) data = json.loads(r.text) patch_data = urlopen(patch_url) patch_file = '{}.patch'.format(pull_request_id) patch_path = '{}/{}/'.format(config.patchFolderPath, pull_request_id) if not os.path.exists(os.path.dirname(patch_path)): os.makedirs(os.path.dirname(patch_path)) f = open(patch_path + patch_file, 'w') f.write(patch_data.read().decode('utf-8')) f.close() gitRepository.apply("{}/{}".format(patch_path, patch_file)) filelist = [] for changed_file in data: this_file = { 'filename': changed_file['filename'], 'status': changed_file['status'], 'built': False, 'built_path': '' } try: if this_file['status'] != 'removed': filename = this_file['filename'] if not os.path.exists( os.path.dirname(config.ciRepoPath + '/' + str(pull_request_id) + '/' + filename)): os.makedirs( os.path.dirname(config.ciRepoPath + '/' + str(pull_request_id) + '/' + filename)) markdown.markdownFromFile( input=config.localRepoPath + '/' + changed_file['filename'], output="{}/{}/{}.html".format(config.ciRepoPath, pull_request_id, filename), output_format="html5", extensions=['markdown.extensions.fenced_code']) this_file['built_path'] = "{}/{}.html".format( pull_request_id, filename) this_file['built'] = True finally: filelist.append(this_file) gitRepository.stash() filelist_name = "filelist-pr-{}.json".format(pull_request_id) with open(patch_path + '/' + filelist_name, 'w') as f: json.dump(filelist, f)
def load_legal_doc(doc_name, locale): """ Return the HTML content of a legal doc in the requested locale. :param doc_name: name of the legal doc folder :param locale: preferred language version of the doc :return: dict containing string content of the file (or None), a boolean value indicating whether the file is localized into the specified locale, and a dict of all available locales for that document """ # for file access convert bedrock locale to legal-docs equivalent if locale in BEDROCK_LOCALES_TO_LEGAL_DOCS: locale = BEDROCK_LOCALES_TO_LEGAL_DOCS[locale] source_dir = path.join(LEGAL_DOCS_PATH, doc_name) source_file = path.join(source_dir, locale + '.md') output = StringIO.StringIO() locales = [f.replace('.md', '') for f in listdir(source_dir) if f.endswith('.md')] # convert legal-docs locales to bedrock equivalents locales = [LEGAL_DOCS_LOCALES_TO_BEDROCK.get(l, l) for l in locales] # filter out non-production locales and convert to dict with names translations = get_translations_native_names(locales) localized = locale != settings.LANGUAGE_CODE # it's possible the legal-docs repo changed the filename to match our locale. # this makes it work for mapped locales even if the map becomes superfluous. if not path.exists(source_file) and locale in LEGAL_DOCS_LOCALES_TO_BEDROCK: locale = LEGAL_DOCS_LOCALES_TO_BEDROCK[locale] source_file = path.join(source_dir, locale + '.md') if not path.exists(source_file): source_file = path.join(LEGAL_DOCS_PATH, doc_name, 'en-US.md') localized = False try: # Parse the Markdown file md.markdownFromFile(input=source_file, output=output, extensions=['attr_list', 'headerid', OutlineExtension((('wrapper_cls', ''),))]) content = output.getvalue().decode('utf8') except IOError: content = None localized = False finally: output.close() return { 'content': content, 'localized': localized, 'translations': translations, }
def run(): """Run Markdown from the command line.""" # Parse options and adjust logging level if necessary options, logging_level = parse_options() if not options: sys.exit(2) logger.setLevel(logging_level) logger.addHandler(logging.StreamHandler()) # Run try: markdown.markdownFromFile(**options) except IOError, e: logging.error(e)
def markdown(template_path, _): ''' render markdown file ''' import io import markdown as md from drape import config dirpath = config.MARKDOWN_DIR filepath = '%s/%s.md' % (dirpath, template_path) output = io.StringIO() md.markdownFromFile(filepath, output) ret = output.getvalue().decode('utf-8') return ret
def md2html(md_file): """ Converts a Markdown file to HTML """ dir_name = os.path.dirname(md_file) file_name = os.path.splitext(os.path.basename(md_file))[0] html_file = f"{os.path.join(dir_name,file_name)}.html" md.markdownFromFile(input=md_file, output=html_file) with open(html_file, "r+", encoding="utf-8") as f: html_markup = f"<html>\n\t<head>\n\t\t<title>{file_name}</title>\n\t</head>\n\t<body>\n{f.read()}\n\t</body>\n</html>" f.seek(0) f.write(html_markup) return html_file
def generate(file, config): """ Write expected output file for given input. """ cfg_section = get_section(file, config) if config.get(cfg_section, 'skip') or config.get(cfg_section, 'normalize'): print('Skipping:', file) return None input_file = file + config.get(cfg_section, 'input_ext') output_file = file + config.get(cfg_section, 'output_ext') if not os.path.isfile(output_file) or \ os.path.getmtime(output_file) < os.path.getmtime(input_file): print('Generating:', file) markdown.markdownFromFile(input=input_file, output=output_file, encoding='utf-8', **get_args(file, config)) else: print('Already up-to-date:', file)
def generate(file, config): """ Write expected output file for given input. """ cfg_section = get_section(file, config) if config.get(cfg_section, 'skip'): print 'Skipping:', file return None input_file = file + config.get(cfg_section, 'input_ext') output_file = file + config.get(cfg_section, 'output_ext') if not os.path.isfile(output_file) or \ os.path.getmtime(output_file) < os.path.getmtime(input_file): print 'Generating:', file markdown.markdownFromFile(input=input_file, output=output_file, encoding='utf-8', **get_args(file, config)) else: print 'Already up-to-date:', file
def handle_moar(topic): topic = str(topic) filename = topic.lower().replace(' ', '_') label = 'modal{}Label'.format(topic.replace(' ', '')) dir_path = os.path.dirname(os.path.realpath(__file__)) full_path = '{}/static/markdown/{}.md'.format(dir_path, filename) if os.path.isfile(full_path): md = StringIO.StringIO() markdown.markdownFromFile(input=full_path, output=md) moar = Markup(md.getvalue()) md.close() else: moar = "That's odd, there doesn't seem to be anything moar about {}".format( topic) return render_template('moar.html', label=label, topic=topic, moar=moar)
def __init__(self, path): self.in_code, self.code = False, '' self.path = Path(path) / type(self).template_name self.html = self.path.with_name('index.html') with self.html.open('w'): pass self.py = self.path.with_name('__init__.py') with self.py.open('w'): pass tempfile = self.path.with_suffix('.html') with tempfile.open('w'): pass markdown.markdownFromFile(input=str(self.path), output=str(tempfile)) super().__init__() self.open_module() with tempfile.open(encoding='utf-8') as temp: self.feed(temp.read()) self.close_module()
def markdown2html(): SOURCE_FILES_PATH = os.path.join(MD_PATH, "*.md") SOURCE_FILES = glob.glob(SOURCE_FILES_PATH) for pos in range(0, len(SOURCE_FILES)): file_base_name = os.path.basename(SOURCE_FILES[pos]) file_name = file_base_name.replace(".md", "") output_files_path = os.path.join(MD_PATH, file_name + ".html") markdown.markdownFromFile(input=SOURCE_FILES[pos], output=output_files_path, encoding="utf-8", extensions=[ 'markdown.extensions.fenced_code', 'markdown.extensions.tables' ], output_format="html5")
def convert_to_html(self) -> None: md_destination = str(self.destination) + '.md' html_destination = str(self.destination) + '.html' data = markdown.markdownFromFile(input=md_destination, output=html_destination, encoding="utf-8", extensions=['fenced_code', 'tables'])
def read(self): content = StringIO.StringIO() markdown.markdownFromFile(input = self.filename, \ output = content, \ extensions = ['codehilite']) getIp = ip.ip().getIpInfo() instance = handleSta.handleSta() instance.read() return jinja2_template('templates/article.html', \ domain = settings.domain, \ article_content = content.getvalue().decode('utf-8'), \ title = self.read_title(), \ pub_time = self.read_time(), \ ipInfo = getIp, \ pv = instance.getValue('pv', 0))
def _compileMarkDownFile(self, label_index=None): """ Function to compile the markdown file. Uses markdown's markdownFromFile method and saves it to the file name generated from _getFile """ inputFileName = self._getFileName(label_index) outputFileName = self._getFileName(label_index, 'html') html_output = markdownFromFile(inputFileName, outputFileName)
def gendoc(fpath, md_extensions = []): """generate html doc as string from source markdown file""" if not path.isfile (fpath): """check path is a file""" bottle.abort(400, 'invalid file: %s' % fpath) @bottle.view('htdoc_head') @tpl_data(fpath) @tpl_utils def htdoc_head(): """render htdoc head template""" return dict() @bottle.view('htdoc_tail') @tpl_data(fpath) @tpl_utils def htdoc_tail(): """render htdoc tail template""" return dict() # parse markdown file buf = io.BytesIO() try: markdownFromFile(input = fpath, extensions = md_extensions, output = buf, output_format = 'html5') except FileNotFoundError as err: bottle.abort(404, str(err)) else: buf.seek(0, 0) # generate response return htdoc_head() + buf.read().decode() + htdoc_tail()