Skip to content

Commit

Permalink
complete a
Browse files Browse the repository at this point in the history
  • Loading branch information
BaseMax committed Jan 8, 2025
1 parent 1b648dd commit 7a289d2
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 16 deletions.
11 changes: 7 additions & 4 deletions build/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
from type import Type

class Attribute:
def __init__(self, generate_name):
self.generate_name = generate_name
def __init__(self):
self.text = Text()
self.id = "AST_LAYOUT_ATTRIBUTE_" + generate_name.upper()
self.type = Type.String
self.reserved_values = []

def set_generate_name(self, generate_name):
"""Set the generate name for the main element."""
self.generate_name = generate_name
self.id = "AST_LAYOUT_ATTRIBUTE_TYPE_" + generate_name.upper().replace(" ", "_").replace("-", "_").replace("__", "_")
return self

def set_type(self, type):
Expand All @@ -32,6 +31,8 @@ def add_reserve_value(self, value):
def validate(self):
"""Check if all languages have a value for the text."""
self.text.validate()
if not self.generate_name:
raise ValueError("Attribute generate name is not set.")
return self

def to_dict(self):
Expand All @@ -41,5 +42,7 @@ def to_dict(self):
"generate_name": self.generate_name,
"type": self.type,
"text": self.text.to_dict(),
"reserved_values": self.reserved_values,
"reserved_values": [
value.to_dict() for value in self.reserved_values
],
}
10 changes: 5 additions & 5 deletions build/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ def __init__(self):
self.attributes = []
self.is_mother = False

def is_mother(self, is_mother):
def set_is_mother(self, value):
"""Set the mother status for the main element."""
self.is_mother = is_mother
self.is_mother = value

def set_generate_name(self, generate_name):
"""Set the generate name for the main element."""
self.generate_name = generate_name
self.id = "AST_LAYOUT_TYPE_" + generate_name.upper()
self.id = "AST_LAYOUT_TYPE_" + generate_name.upper().replace(" ", "_").replace("-", "_").replace("__", "_")

def set_text(self, language_code, text):
"""Set text for the main element."""
self.text.add_translation(language_code, text)

def add_attribute(self, attribute_generate_name):
def add_attribute(self):
"""Add an attribute with its own text translations."""
attribute = Attribute(attribute_generate_name)
attribute = Attribute()
self.attributes.append(attribute)

return attribute
Expand Down
114 changes: 108 additions & 6 deletions build/tags/a.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,124 @@
from element import Element
from lang import Lang
from type import Type
from value import Value

if __name__ == "__main__":
element = Element()
element.is_mother(True)
element.set_is_mother(True)
element.set_generate_name("a")
element.set_text(Lang.EN, "link")
element.set_text(Lang.FA, "لینک")

element.add_attribute() \
.set_generate_name("href") \
.set_type(Type.Url) \
.add_text(Lang.EN, "href") \
.add_text(Lang.FA, "لینک")

element.add_attribute() \
.set_generate_name("target") \
.set_type(Type.String) \
.add_text(Lang.EN, "target") \
.add_text(Lang.FA, "هدف") \
.add_reserve_value(Value().set_generate_name("_blank") \
.add_text(Lang.EN, "blank") \
.add_text(Lang.FA, "جدید")
) \
.add_reserve_value(Value().set_generate_name("_self") \
.add_text(Lang.EN, "self") \
.add_text(Lang.FA, "خود")
) \
.add_reserve_value(Value().set_generate_name("_parent") \
.add_text(Lang.EN, "parent") \
.add_text(Lang.FA, "والدین")
) \
.add_reserve_value(Value().set_generate_name("_top") \
.add_text(Lang.EN, "top") \
.add_text(Lang.FA, "بالا")
)

element.add_attribute() \
.set_generate_name("rel") \
.set_type(Type.String) \
.add_text(Lang.EN, "rel") \
.add_text(Lang.FA, "رابطه") \
.add_reserve_value(Value().set_generate_name("noopener") \
.add_text(Lang.EN, "noopener") \
.add_text(Lang.FA, "بدون بازکردن")
) \
.add_reserve_value(Value().set_generate_name("noreferrer") \
.add_text(Lang.EN, "noreferrer") \
.add_text(Lang.FA, "بدون ارجاع")
) \
.add_reserve_value(Value().set_generate_name("external") \
.add_text(Lang.EN, "external") \
.add_text(Lang.FA, "خارجی")
)

element.add_attribute() \
.set_generate_name("download") \
.set_type(Type.String) \
.add_text(Lang.EN, "download") \
.add_text(Lang.FA, "دانلود") \
.add_reserve_value(Value().set_generate_name("file") \
.add_text(Lang.EN, "file") \
.add_text(Lang.FA, "فایل")
)

element.add_attribute() \
.set_generate_name("hreflang") \
.set_type(Type.String) \
.add_text(Lang.EN, "hreflang") \
.add_text(Lang.FA, "زبان لینک") \
.add_reserve_value(Value().set_generate_name("en") \
.add_text(Lang.EN, "en") \
.add_text(Lang.FA, "انگلیسی")
) \
.add_reserve_value(Value().set_generate_name("fa") \
.add_text(Lang.EN, "fa") \
.add_text(Lang.FA, "فارسی")
) \

element.add_attribute() \
.set_generate_name("media") \
.set_type(Type.AnyTodo) \
.add_text(Lang.EN, "media") \
.add_text(Lang.FA, "رسانه") \

element.add_attribute() \
.set_generate_name("ping") \
.set_type(Type.AnyTodo) \
.add_text(Lang.EN, "ping") \
.add_text(Lang.FA, "پینگ") \

element.add_attribute() \
.set_generate_name("referrerpolicy") \
.set_type(Type.String) \
.add_text(Lang.EN, "referrerpolicy") \
.add_text(Lang.FA, "سیاست ارجاع") \
.add_reserve_value(Value().set_generate_name("no-referrer") \
.add_text(Lang.EN, "no-referrer") \
.add_text(Lang.FA, "بدون ارجاع")
) \
.add_reserve_value(Value().set_generate_name("origin") \
.add_text(Lang.EN, "origin") \
.add_text(Lang.FA, "منبع")
)

element.add_attribute() \
.set_generate_name("type") \
.set_type(Type.String) \
.add_text(Lang.EN, "url") \
.add_text(Lang.FA, "آدرس") \
.add_reserve_value(Value(

))
.add_text(Lang.EN, "type") \
.add_text(Lang.FA, "نوع") \
.add_reserve_value(Value().set_generate_name("text/html") \
.add_text(Lang.EN, "text/html") \
.add_text(Lang.FA, "text/html")
) \
.add_reserve_value(Value().set_generate_name("application/pdf") \
.add_text(Lang.EN, "application/pdf") \
.add_text(Lang.FA, "application/pdf")
)

element_dict = element.to_dict()

Expand Down
4 changes: 3 additions & 1 deletion build/type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Type:
"""Type of an attribute."""
Any = "AST_TYPE_ANY"
AnyTodo = "AST_TYPE_ANY"
String = "AST_TYPE_STRING"
Url = "AST_TYPE_URL"
Number = "AST_TYPE_NUMBER"
Boolean = "AST_TYPE_BOOLEAN"
Any = "AST_TYPE_ANY"
33 changes: 33 additions & 0 deletions build/value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from text import Text

class Value:
def __init__(self):
self.text = Text()
self.generate_name = None

def set_generate_name(self, generate_name):
"""Set the generate name for the attribute value"""
self.generate_name = generate_name
self.id = "AST_LAYOUT_ATTRIBUTE_VALUE_TYPE_" + generate_name.upper().replace(" ", "_").replace("-", "_").replace("__", "_")

return self

def add_text(self, language_code, text):
"""Set text for the main element."""
self.text.add_translation(language_code, text)
return self

def validate(self):
"""Check if all languages have a value for the text."""
self.text.validate()
if not self.generate_name:
raise ValueError("The generate name is not set.")
return self

def to_dict(self):
"""Convert the attribute value to a dictionary, including all information."""
return {
"id": self.id,
"generate_name": self.generate_name,
"text": self.text.to_dict(),
}

0 comments on commit 7a289d2

Please sign in to comment.