def take_action(self, parsed_args): self.app.stdout.write('Building ginger site...\n') print "Watch for changes? %s" % parsed_args.watch current_directory = os.getcwd() views_directory = current_directory + '/_views' layouts_directory = current_directory + '/_layouts' env = Environment(loader=FileSystemLoader(layouts_directory), autoescape=False) try: stream = open(current_directory+'/_config.yaml', 'r') _config = yaml.load(stream) except IOError: self.app.stdout.write('No _config.yaml file found. Unable' ' to build site.\n Please run ginger' ' new in order to create site.\n') sys.exit(1) site_data = { 'title': _config['title'], 'author': _config['author'], 'description': _config['description'], 'style': _config['style'], 'views': [] } for filename in os.listdir(views_directory): if filename != ".DS_Store": filepath = views_directory+"/"+filename print filepath this_view = open(filepath, 'r').read() #print this_view exp = r'^(-{3}(?:\n|\r)([\w\W]+?)-{3})?([\w\W]*)' parsed_page = re.search(exp, this_view) view_data = yaml.load(parsed_page.group(2)) view_data['template'] = parsed_page.group(3).strip() site_data['views'].append(view_data) self.app.stdout.write(' - Parsing %s\n' % view_data['title']) template = env.get_template( '%s.html' % _config['style'] ) json_data = json.dumps(site_data) rendered_layout = template.render(site_data=site_data, json_data=json_data) with open('index.html', 'wb') as html_output: html_output.write(rendered_layout) rendered_css = sass.compile_file('_scss/main.scss') with open('css/main.css', 'wb') as css_output: css_output.write(rendered_css)
def compile_write(scsspath, csspath): 'Call libsass wrapper with the scsspath, (create and) write to csspath' cssdir = os.path.dirname(csspath) if not os.path.exists(cssdir): os.makedirs(cssdir) with open(csspath, 'w') as cssfile: css = sass.compile_file(bytes(scsspath, 'utf-8')).decode('utf-8') cssfile.write(css)
def compile_scss_file(input_file, output_file): if not path.exists(input_file): raise IOError('%s does not exist' % (input_file,)) output_path = path.split(output_file)[0] if not path.exists(output_path): makedirs(output_path) with open(output_file, 'w') as css_file: css_file.write(compile_file(input_file))
def take_action(self, parsed_args): self.app.stdout.write('Building ginger site...\n') print "Watch for changes? %s" % parsed_args.watch current_directory = os.getcwd() views_directory = current_directory + '/_views' layouts_directory = current_directory + '/_layouts' env = Environment(loader=FileSystemLoader(layouts_directory), autoescape=False) try: stream = open(current_directory + '/_config.yaml', 'r') _config = yaml.load(stream) except IOError: self.app.stdout.write('No _config.yaml file found. Unable' ' to build site.\n Please run ginger' ' new in order to create site.\n') sys.exit(1) site_data = { 'title': _config['title'], 'author': _config['author'], 'description': _config['description'], 'style': _config['style'], 'views': [] } for filename in os.listdir(views_directory): if filename != ".DS_Store": filepath = views_directory + "/" + filename print filepath this_view = open(filepath, 'r').read() #print this_view exp = r'^(-{3}(?:\n|\r)([\w\W]+?)-{3})?([\w\W]*)' parsed_page = re.search(exp, this_view) view_data = yaml.load(parsed_page.group(2)) view_data['template'] = parsed_page.group(3).strip() site_data['views'].append(view_data) self.app.stdout.write(' - Parsing %s\n' % view_data['title']) template = env.get_template('%s.html' % _config['style']) json_data = json.dumps(site_data) rendered_layout = template.render(site_data=site_data, json_data=json_data) with open('index.html', 'wb') as html_output: html_output.write(rendered_layout) rendered_css = sass.compile_file('_scss/main.scss') with open('css/main.css', 'wb') as css_output: css_output.write(rendered_css)
def _compile(self, filename): input_file = os.path.join( current_app.root_path, self._files[filename] ).encode() return sass.compile_file( input_file, include_paths=self._include_paths, output_style=self._output_style )
def compile_sass(scss_file, css_file): import sass if not need_to_compile(scss_file, css_file): return try: result = sass.compile_file(scss_file.encode()) with open(css_file, 'wb') as fout: fout.write(result) except Exception as ex: raise CompileException(str(ex))
def compile_file(self, infile, outfile, outdated=False, force=False): self.include_paths.append(os.path.dirname(infile)) if force or outdated: self.save_file(self.raw_path, sass.compile_file(str(infile), ','.join(self.include_paths)))
def test_compile_file(self): result = sass.compile_file(scss_test_file) self.assertEqual(result, compiled_result)
#!/usr/bin/python # # Update CSS via Python sass module # # Note: This script now uses the libsass Python module. import codecs import os import sass for entry in os.listdir("."): if entry[0] in (".", "_"): continue base, ext = os.path.splitext(entry) if ext != ".scss": continue try: css_content = sass.compile_file(entry, output_style=sass.SASS_STYLE_COMPRESSED) # css_content = sass.compile(filename=entry, output_style='compressed') # either nested or compressed #with open(os.path.join("static", base + ".css"), "w") as output_fd: with codecs.open(os.path.join("static", base + ".css"),'w',encoding='utf8') as output_fd: output_fd.write(css_content) except Exception, e: print "Error processing %s: %s" % (entry, e)