From 7a289d2406e14b2b41c86580e7b3598fb4fbb0a8 Mon Sep 17 00:00:00 2001 From: "ALI Mohammadiyeh (Max Base)" Date: Wed, 8 Jan 2025 22:57:18 +0100 Subject: [PATCH] complete a --- build/attribute.py | 11 +++-- build/element.py | 10 ++-- build/tags/a.py | 114 ++++++++++++++++++++++++++++++++++++++++++--- build/type.py | 4 +- build/value.py | 33 +++++++++++++ 5 files changed, 156 insertions(+), 16 deletions(-) diff --git a/build/attribute.py b/build/attribute.py index 35ce6428..c4f88d97 100644 --- a/build/attribute.py +++ b/build/attribute.py @@ -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): @@ -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): @@ -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 + ], } diff --git a/build/element.py b/build/element.py index f63f42c2..c065800b 100644 --- a/build/element.py +++ b/build/element.py @@ -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 diff --git a/build/tags/a.py b/build/tags/a.py index 84fed914..11af1317 100644 --- a/build/tags/a.py +++ b/build/tags/a.py @@ -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() diff --git a/build/type.py b/build/type.py index 4657c7d2..a746c1ed 100644 --- a/build/type.py +++ b/build/type.py @@ -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" diff --git a/build/value.py b/build/value.py index e69de29b..87524340 100644 --- a/build/value.py +++ b/build/value.py @@ -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(), + }