# Mecha CMS

## Standard Generalized Markup Language

Because `DOMDocument` API isn’t enough for me.

There will be a plan to remove the `Union` class in the future because of its name which does not describe its function properly. The `Union` class will be replaced by a new class named `SGML` so that it will be easier for developers to create derived classes from that class. In the future, `HTML` and `Block` class functionality will be derived from this class too.

``````class HTML extends SGML {}
class Block extends SGML {}``````

This class doesn’t have any public methods and only serves as a helper to create SGML elements (which is very similar to the HTML writing style) from an array input, and vice versa, to convert SGML element string into array.

``````\$foo = new SGML;
\$foo = 'foo';
\$foo = 'bar'
\$foo['baz'] = 'qux';

echo \$foo; // `<foo baz="qux">bar</foo>```````
``````\$foo = new SGML('<foo baz="qux">bar</foo>');

echo To::JSON(\$foo); // `["foo","bar",{"baz":"qux"}]```````

There will be no such standard way to make SGML comments markup through the class constructor because this markup is too easy to be written by hand. The case will be different when we want to add or remove some HTML or block attributes directly through its string representation. And that’s why I made this class. However, developers can still add their own custom methods dynamically through the `Genome` class feature, or by defining those methods directly. For example, here, a developer can add a `comment` method to make it easier for users to write HTML comments:

``````class HTML extends SGML {

public static function comment(string \$text) {
return '<!-- ' . \$text . ' -->';
}

}

SGML::_('comment', function(string \$text) {
return '<!-- ' . \$text . ' -->';
});

// Example
echo HTML::comment('asdf');
echo SGML::comment('asdf');``````

Perhaps, one interesting advantage of this class implementation is that we can create some methods to help us in manipulating the attributes and contents of the HTML string provided to the class constructor:

``````class HTML extends SGML {

public function addClass(string \$name) {
\$class = \$this->lot['class'];
\$class = \$class === true ? [] : explode(' ', \$class);
\$class[] = \$name;
\$class = array_unique(\$class);
\$this->lot['class'] = implode(' ', \$class);
return \$this;
}

public function hasAttribute(string \$name) {
return array_key_exists(\$name, \$this->lot);
}

public function hasClass(string \$name) {
if (isset(\$this->lot['class'])) {
\$class = \$this->lot['class'];
if (\$class === true) {
return false;
}
\$class = ' ' . \$class . ' ';
return strpos(\$class, ' ' . \$name . ' ') !== false;
}
return false;
}

public function removeAttribute(string \$name) {
unset(\$this->lot[\$name]);
return \$this;
}

public function removeClass(string \$name) {
if (isset(\$this->lot['class'])) {
\$class = \$this->lot['class'];
if (\$class === true) {
return \$this;
}
\$class = ' ' . \$class . ' ';
\$class = str_replace(' ' . \$name . ' ', ' ', \$class);
\$this->lot['class'] = trim(\$class);
}
return \$this;
}

public function setAttribute(string \$name, \$value) {
\$this->lot[\$name] = \$value;
return \$this;
}

public function setContent(string \$content) {
\$this->lot = \$content;
return \$this;
}

public function toggleClass(string \$name) {
\$toggle = \$this->hasClass(\$name) ? 'removeClass' : 'addClass';
return \$this->{\$toggle}(\$name);
}

}``````

Example:

``````\$html = new HTML('<div></div>');

\$html->setAttribute('title', 'Hello!');
\$html->setContent('Content goes here…');
{"ft_lang_label":"__label__en","ft_lang_prob":0.5545261,"math_prob":0.5697899,"size":3529,"snap":"2019-13-2019-22","text_gpt3_token_len":898,"char_repetition_ratio":0.1574468,"word_repetition_ratio":0.104961835,"special_character_ratio":0.31226978,"punctuation_ratio":0.15797547,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9569178,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-03-19T18:41:56Z\",\"WARC-Record-ID\":\"<urn:uuid:5670c5c4-5da8-4357-9f70-01f53ade8960>\",\"Content-Length\":\"10466\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:018ce532-fbfb-4168-ac5d-51d72e4dc8ac>\",\"WARC-Concurrent-To\":\"<urn:uuid:17523fa0-e7e7-470d-9ed7-0cb1a05ab0e3>\",\"WARC-IP-Address\":\"104.156.54.157\",\"WARC-Target-URI\":\"https://mecha-cms.com/article/s-g-m-l\",\"WARC-Payload-Digest\":\"sha1:WE6HAC2E53ROXLODZSFL6NRQTOGVWO3W\",\"WARC-Block-Digest\":\"sha1:D5EJ45C3JOMCD6ZNNGT5LLYW77HT3SJX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-13/CC-MAIN-2019-13_segments_1552912202125.41_warc_CC-MAIN-20190319183735-20190319205735-00199.warc.gz\"}"}