|
| 1 | +#!/usr/bin/env python |
| 2 | +__author__ = 'Tony Beltramelli - www.tonybeltramelli.com' |
| 3 | + |
| 4 | +import json |
| 5 | +from classes.Node import * |
| 6 | + |
| 7 | + |
| 8 | +class Compiler: |
| 9 | + def __init__(self, dsl_mapping_file_path): |
| 10 | + with open(dsl_mapping_file_path) as data_file: |
| 11 | + self.dsl_mapping = json.load(data_file) |
| 12 | + |
| 13 | + self.opening_tag = self.dsl_mapping["opening-tag"] |
| 14 | + self.closing_tag = self.dsl_mapping["closing-tag"] |
| 15 | + self.css_file_name = self.dsl_mapping["css-file-name"] |
| 16 | + self.content_holder = self.opening_tag + self.closing_tag |
| 17 | + |
| 18 | + self.root = Node("body", None, self.content_holder) |
| 19 | + |
| 20 | + def compile(self, input_file_path, output_file_path, rendering_function=None): |
| 21 | + dsl_file = open(input_file_path) |
| 22 | + current_parent = self.root |
| 23 | + |
| 24 | + for token in dsl_file: |
| 25 | + token = token.replace(" ", "").replace("\n", "") |
| 26 | + |
| 27 | + if token.find(self.opening_tag) != -1: |
| 28 | + token = token.replace(self.opening_tag, "") |
| 29 | + |
| 30 | + element = Node(token, current_parent, self.content_holder) |
| 31 | + current_parent.add_child(element) |
| 32 | + current_parent = element |
| 33 | + elif token.find(self.closing_tag) != -1: |
| 34 | + current_parent = current_parent.parent |
| 35 | + else: |
| 36 | + tokens = token.split(",") |
| 37 | + for t in tokens: |
| 38 | + element = Node(t, current_parent, self.content_holder) |
| 39 | + current_parent.add_child(element) |
| 40 | + |
| 41 | + output_html = self.root.render(self.dsl_mapping, rendering_function=rendering_function) |
| 42 | + with open(output_file_path, 'w') as output_file: |
| 43 | + output_file.write(output_html) |
| 44 | + |
| 45 | + # Checking for Errors |
| 46 | + if output_html is None: |
| 47 | + return "Parsing Error" |
| 48 | + |
| 49 | + with open(output_file_path, 'w') as output_file: |
| 50 | + output_file.truncate(0) |
| 51 | + output_file.write(output_html) |
| 52 | + |
| 53 | + output_file.close() |
| 54 | + return output_html |
0 commit comments