Ejemplo n.º 1
0
def render_include(content):
	'''render {% raw %}{% include "app/path/filename" %}{% endraw %} in js file'''

	content = cstr(content)

	# try 5 levels of includes
	for i in range(5):
		if "{% include" in content:
			paths = re.findall(r'''{% include\s['"](.*)['"]\s%}''', content)
			if not paths:
				frappe.throw(_('Invalid include path'), InvalidIncludePath)

			for path in paths:
				app, app_path = path.split('/', 1)
				with io.open(frappe.get_app_path(app, app_path), 'r', encoding = 'utf-8') as f:
					include = f.read()
					if path.endswith('.html'):
						include = html_to_js_template(path, include)

					content = re.sub(r'''{{% include\s['"]{0}['"]\s%}}'''.format(path), include, content)

		else:
			break

	return content
Ejemplo n.º 2
0
	def load_assets(self):
		from frappe.modules import get_module_path, scrub
		import os

		path = os.path.join(get_module_path(self.module), 'page', scrub(self.name))

		# script
		fpath = os.path.join(path, scrub(self.name) + '.js')
		if os.path.exists(fpath):
			with open(fpath, 'r') as f:
				self.script = unicode(f.read(), "utf-8")

		# css
		fpath = os.path.join(path, scrub(self.name) + '.css')
		if os.path.exists(fpath):
			with open(fpath, 'r') as f:
				self.style = unicode(f.read(), "utf-8")

		# html as js template
		for fname in os.listdir(path):
			if fname.endswith(".html"):
				with open(os.path.join(path, fname), 'r') as f:
					template = unicode(f.read(), "utf-8")
					self.script = html_to_js_template(fname, template) + self.script

		if frappe.lang != 'en':
			from frappe.translate import get_lang_js
			self.script += get_lang_js("page", self.name)
Ejemplo n.º 3
0
def render_include(content):
	"""render {% raw %}{% include "app/path/filename" %}{% endraw %} in js file"""

	content = cstr(content)

	# try 5 levels of includes
	for i in range(5):
		if "{% include" in content:
			paths = re.findall(r"""{% include\s['"](.*)['"]\s%}""", content)
			if not paths:
				frappe.throw(_("Invalid include path"), InvalidIncludePath)

			for path in paths:
				app, app_path = path.split("/", 1)
				with io.open(frappe.get_app_path(app, app_path), "r", encoding="utf-8") as f:
					include = f.read()
					if path.endswith(".html"):
						include = html_to_js_template(path, include)

					content = re.sub(r"""{{% include\s['"]{0}['"]\s%}}""".format(path), include, content)

		else:
			break

	return content
Ejemplo n.º 4
0
def render_include(content):
	'''render {% include "app/path/filename" in js file %}'''

	content = cstr(content)

	# try 5 levels of includes
	for i in xrange(5):
		if "{% include" in content:
			paths = re.findall(r'''{% include\s['"](.*)['"]\s%}''', content)
			if not paths:
				frappe.throw('Invalid include path')

			for path in paths:
				app, app_path = path.split('/', 1)
				with open(frappe.get_app_path(app, app_path), 'r') as f:
					include = unicode(f.read(), 'utf-8')
					if path.endswith('.html'):
						include = html_to_js_template(path, include)

					content = re.sub(r'''{{% include\s['"]{0}['"]\s%}}'''.format(path), include, content)

		else:
			break

	return content
Ejemplo n.º 5
0
    def load_assets(self):
        import os

        from frappe.modules import get_module_path, scrub

        self.script = ""

        page_name = scrub(self.name)

        path = os.path.join(get_module_path(self.module), "page", page_name)

        # script
        fpath = os.path.join(path, page_name + ".js")
        if os.path.exists(fpath):
            with open(fpath, "r") as f:
                self.script = render_include(f.read())
                self.script += f"\n\n//# sourceURL={page_name}.js"

        # css
        fpath = os.path.join(path, page_name + ".css")
        if os.path.exists(fpath):
            with open(fpath, "r") as f:
                self.style = safe_decode(f.read())

        # html as js template
        for fname in os.listdir(path):
            if fname.endswith(".html"):
                with open(os.path.join(path, fname), "r") as f:
                    template = f.read()
                    if "<!-- jinja -->" in template:
                        context = frappe._dict({})
                        try:
                            out = frappe.get_attr(
                                "{app}.{module}.page.{page}.{page}.get_context"
                                .format(app=frappe.local.module_app[scrub(
                                    self.module)],
                                        module=scrub(self.module),
                                        page=page_name))(context)

                            if out:
                                context = out
                        except (AttributeError, ImportError):
                            pass

                        template = frappe.render_template(template, context)
                    self.script = html_to_js_template(fname,
                                                      template) + self.script

                    # flag for not caching this page
                    self._dynamic_page = True

        if frappe.lang != "en":
            from frappe.translate import get_lang_js

            self.script += get_lang_js("page", self.name)

        for path in get_code_files_via_hooks("page_js", self.name):
            js = get_js(path)
            if js:
                self.script += "\n\n" + js
Ejemplo n.º 6
0
    def load_assets(self):
        from frappe.modules import get_module_path, scrub
        import os

        path = os.path.join(get_module_path(self.module), 'page',
                            scrub(self.name))

        # script
        fpath = os.path.join(path, scrub(self.name) + '.js')
        if os.path.exists(fpath):
            with open(fpath, 'r') as f:
                self.script = unicode(f.read(), "utf-8")

        # css
        fpath = os.path.join(path, scrub(self.name) + '.css')
        if os.path.exists(fpath):
            with open(fpath, 'r') as f:
                self.style = unicode(f.read(), "utf-8")

        # html as js template
        for fname in os.listdir(path):
            if fname.endswith(".html"):
                with open(os.path.join(path, fname), 'r') as f:
                    template = unicode(f.read(), "utf-8")
                    self.script = html_to_js_template(fname,
                                                      template) + self.script

        if frappe.lang != 'en':
            from frappe.translate import get_lang_js
            self.script += get_lang_js("page", self.name)
Ejemplo n.º 7
0
	def add_html_templates(self, path):
		if self.custom:
			return
		js = ""
		for fname in os.listdir(path):
			if fname.endswith(".html"):
				with open(os.path.join(path, fname), 'r') as f:
					template = unicode(f.read(), "utf-8")
					js += html_to_js_template(fname, template)

		self.set("__js", (self.get("__js") or "") + js)
Ejemplo n.º 8
0
	def add_html_templates(self, path):
		if self.custom:
			return
		js = ""
		for fname in os.listdir(path):
			if fname.endswith(".html"):
				with open(os.path.join(path, fname), 'r') as f:
					template = unicode(f.read(), "utf-8")
					js += html_to_js_template(fname, template)

		self.set("__js", (self.get("__js") or "") + js)
Ejemplo n.º 9
0
	def load_assets(self):
		from frappe.modules import get_module_path, scrub
		import os
		self.script = ''

		page_name = scrub(self.name)

		path = os.path.join(get_module_path(self.module), 'page', page_name)

		# script
		fpath = os.path.join(path, page_name + '.js')
		if os.path.exists(fpath):
			with open(fpath, 'r') as f:
				self.script = render_include(f.read())

		# css
		fpath = os.path.join(path, page_name + '.css')
		if os.path.exists(fpath):
			with open(fpath, 'r') as f:
				self.style = safe_decode(f.read())

		# html as js template
		for fname in os.listdir(path):
			if fname.endswith(".html"):
				with open(os.path.join(path, fname), 'r') as f:
					template = f.read()
					if "<!-- jinja -->" in template:
						context = frappe._dict({})
						try:
							out = frappe.get_attr("{app}.{module}.page.{page}.{page}.get_context".format(
								app = frappe.local.module_app[scrub(self.module)],
								module = scrub(self.module),
								page = page_name
							))(context)

							if out:
								context = out
						except (AttributeError, ImportError):
							pass

						template = frappe.render_template(template, context)
					self.script = html_to_js_template(fname, template) + self.script

					# flag for not caching this page
					self._dynamic_page = True

		if frappe.lang != 'en':
			from frappe.translate import get_lang_js
			self.script += get_lang_js("page", self.name)

		for path in get_code_files_via_hooks("page_js", self.name):
			js = get_js(path)
			if js:
				self.script += "\n\n" + js
Ejemplo n.º 10
0
    def load_assets(self):
        from frappe.modules import get_module_path, scrub
        import os

        page_name = scrub(self.name)

        path = os.path.join(get_module_path(self.module), 'page', page_name)

        # script
        fpath = os.path.join(path, page_name + '.js')
        if os.path.exists(fpath):
            with open(fpath, 'r') as f:
                self.script = unicode(f.read(), "utf-8")

        # css
        fpath = os.path.join(path, page_name + '.css')
        if os.path.exists(fpath):
            with open(fpath, 'r') as f:
                self.style = unicode(f.read(), "utf-8")

        # html as js template
        for fname in os.listdir(path):
            if fname.endswith(".html"):
                with open(os.path.join(path, fname), 'r') as f:
                    template = unicode(f.read(), "utf-8")
                    if "<!-- jinja -->" in template:
                        context = frappe._dict({})
                        try:
                            out = frappe.get_attr(
                                "{app}.{module}.page.{page}.{page}.get_context"
                                .format(app=frappe.local.module_app[scrub(
                                    self.module)],
                                        module=scrub(self.module),
                                        page=page_name))(context)

                            if out:
                                context = out
                        except (AttributeError, ImportError):
                            pass

                        template = frappe.render_template(template, context)
                    self.script = html_to_js_template(fname,
                                                      template) + self.script

        if frappe.lang != 'en':
            from frappe.translate import get_lang_js
            self.script += get_lang_js("page", self.name)

        for path in get_code_files_via_hooks("page_js", self.name):
            js = get_js(path)
            if js:
                self.script += "\n\n" + js