Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4250 pull request #4

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/project_name/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

from project_name.config import configure_app
from .config import configure_app
from . import converter

db = SQLAlchemy()

Expand All @@ -10,4 +11,5 @@ def create_app(import_name: str, config_name: str = 'default'):
app = Flask(import_name)
configure_app(app, config_name=config_name)
db.init_app(app)
app.register_blueprint(converter.bp)
return app
Empty file removed src/project_name/apps/__init__.py
Empty file.
77 changes: 77 additions & 0 deletions src/project_name/converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import functools
from flask import (
Blueprint, request, jsonify
)

bp = Blueprint('converter', __name__, url_prefix='/')


def convert_key(elem):
id = ""
isId = False
classes = []
isClass = False
tclass = ""
name = ""
for c in elem:
if c == ".":
if len(tclass) > 0:
classes.append(tclass)
tclass = ""
isClass = True
isId = False
continue

if c == "#":
if len(tclass) > 0:
classes.append(tclass)
tclass = ""
isId = True
isClass = False
continue

if not isId and not isClass:
name += c
elif isId:
id += c
elif isClass:
tclass += c

if len(tclass) > 0 and isClass:
classes.append(tclass)
print(classes)
return name, id, classes


def convert_elem(elem):
result = ""
if isinstance(elem, list):
result += "<ul>"
for item in elem:
result += "<li>"
result += convert_elem(item)
result += "</li>"
result += "</ul>"
elif isinstance(elem, dict):
for key in elem.keys():
name, id, classes = convert_key(key)

text_class = ""
for i, cl in enumerate(classes):
text_class += cl
if i < len(classes) - 1:
text_class += " "

result += "<" + name + (" id=\"" + id + "\"" if len(id) > 0 else "") +\
(" class=\""+text_class+"\"" if len(text_class) > 0 else "") + ">"
result += convert_elem(elem.get(key, ''))
result += "</"+name+">"
else:
result += elem
return result


@bp.route('/convert', methods=['POST'])
def convert():
data = request.get_json()
return convert_elem(data)