<commit_before># Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals
import frappe

from frappe.utils import strip_html
from frappe import _
from frappe.website.utils import scrub_relative_urls
from jinja2.utils import concat
from jinja2 import meta
import re

def render_blocks(context):
	"""returns a dict of block name and its rendered content"""

	out = {}

	env = frappe.get_jenv()

	def _render_blocks(template_path):
		source = frappe.local.jloader.get_source(frappe.local.jenv, template_path)[0]
		for referenced_template_path in meta.find_referenced_templates(env.parse(source)):
			if referenced_template_path:
				_render_blocks(referenced_template_path)

		template = frappe.get_template(template_path)
		for block, render in template.blocks.items():
			new_context = template.new_context(context)
			out[block] = scrub_relative_urls(concat(render(new_context)))

	_render_blocks(context["template"])

	out["has_sidebar"] = not (context.get("no_sidebar", 0) or ("<!-- no-sidebar -->" in out.get("content", "")))

	if out.get("has_sidebar"):
		out["sidebar"] = frappe.get_template("templates/includes/sidebar.html").render(context)

	out["no_breadcrumbs"] = context.get("no_breadcrumbs", 0) or ("<!-- no-breadcrumbs -->" in out.get("content", ""))
	out["no_header"] = context.get("no_header", 0) or ("<!-- no-header -->" in out.get("content", ""))

	# default blocks if not found

	# title
	if "<!-- title:" in out.get("content", ""):
		out["title"] = re.findall('<!-- title:([^>]*) -->', out.get("content"))[0].strip()

	if "title" not in out:
		out["title"] = context.get("title")

	# header
	if out["no_header"]:
		out["header"] = ""
	else:
		if "title" not in out and out.get("header"):
			out["title"] = out["header"]

		if not out.get("header") and "<h1" not in out.get("content", ""):
			if out.get("title"):
				out["header"] = out["title"]

		if out.get("header") and not re.findall("<h.>", out["header"]):
			out["header"] = "<h1>" + out["header"] + "</h1>"

	if not out.get("header"):
		out["no_header"] = 1

	out["title"] = strip_html(out.get("title") or "")

	# breadcrumbs
	if not out["no_breadcrumbs"] and "breadcrumbs" not in out:
		out["breadcrumbs"] = scrub_relative_urls(
			frappe.get_template("templates/includes/breadcrumbs.html").render(context))

	# meta
	if "meta_block" not in out:<commit_msg>[website] parse markdown<commit_after># Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals
import frappe

from frappe.utils import strip_html
from frappe import _
from frappe.website.utils import scrub_relative_urls
from jinja2.utils import concat
from jinja2 import meta
from markdown2 import markdown
import re

def render_blocks(context):
	"""returns a dict of block name and its rendered content"""

	out = {}

	env = frappe.get_jenv()

	def _render_blocks(template_path):
		source = frappe.local.jloader.get_source(frappe.local.jenv, template_path)[0]
		for referenced_template_path in meta.find_referenced_templates(env.parse(source)):
			if referenced_template_path:
				_render_blocks(referenced_template_path)

		template = frappe.get_template(template_path)
		for block, render in template.blocks.items():
			new_context = template.new_context(context)
			out[block] = scrub_relative_urls(concat(render(new_context)))

	_render_blocks(context["template"])

	out["has_sidebar"] = not (context.get("no_sidebar", 0) or ("<!-- no-sidebar -->" in out.get("content", "")))

	if out.get("has_sidebar"):
		out["sidebar"] = frappe.get_template("templates/includes/sidebar.html").render(context)

	out["no_breadcrumbs"] = context.get("no_breadcrumbs", 0) or ("<!-- no-breadcrumbs -->" in out.get("content", ""))
	out["no_header"] = context.get("no_header", 0) or ("<!-- no-header -->" in out.get("content", ""))

	# default blocks if not found

	# title
	if "<!-- title:" in out.get("content", ""):
		out["title"] = re.findall('<!-- title:([^>]*) -->', out.get("content"))[0].strip()

	if "title" not in out:
		out["title"] = context.get("title")

	# markdown
	if "<!-- title -->" in out.get("content", ""):
		out["content"] = markdown(out["content"])


	# header
	if out["no_header"]:
		out["header"] = ""
	else:
		if "title" not in out and out.get("header"):
			out["title"] = out["header"]

		if not out.get("header") and "<h1" not in out.get("content", ""):
			if out.get("title"):
				out["header"] = out["title"]

		if out.get("header") and not re.findall("<h.>", out["header"]):
			out["header"] = "<h1>" + out["header"] + "</h1>"

	if not out.get("header"):
		out["no_header"] = 1

	out["title"] = strip_html(out.get("title") or "")

	# breadcrumbs
	if not out["no_breadcrumbs"] and "breadcrumbs" not in out:
		out["breadcrumbs"] = scrub_relative_urls(
			frappe.get_template("templates/includes/breadcrumbs.html").render(context))

	# meta
	if "meta_block" not in out:
{'fraction_non_alphanumeric': 0.1774387796065837, 'fraction_numerical': 0.006222400642312324, 'mean_word_length': 4.9605263157894735, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 20, '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': '7217786', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1706, 'n_words': 390}