<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mrl Alex</title>
    <description>The latest articles on DEV Community by Mrl Alex (@mrldv).</description>
    <link>https://dev.to/mrldv</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2094160%2F546880f3-f4d4-4ceb-b5bc-563fcd8aed14.jpg</url>
      <title>DEV Community: Mrl Alex</title>
      <link>https://dev.to/mrldv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mrldv"/>
    <language>en</language>
    <item>
      <title>How I Built a Mini Programming Language for Automatic Web Page Generation – Step by Step</title>
      <dc:creator>Mrl Alex</dc:creator>
      <pubDate>Wed, 18 Sep 2024 22:43:39 +0000</pubDate>
      <link>https://dev.to/mrldv/how-i-built-a-mini-programming-language-for-automatic-web-page-generation-step-by-step-5g6h</link>
      <guid>https://dev.to/mrldv/how-i-built-a-mini-programming-language-for-automatic-web-page-generation-step-by-step-5g6h</guid>
      <description>&lt;p&gt;Building web pages can often feel repetitive and time-consuming, especially when switching between HTML, CSS, and other technologies. This led me to an idea: to create a simplified language that automates the process of generating web pages, allowing me to write code in a minimalist syntax that my custom parser transforms into HTML and CSS.&lt;/p&gt;

&lt;p&gt;In this article, I'll walk you through how I built a custom programming language using Python, explain how the parser works to interpret this language, and show how I simplified the web development process. If you're curious about how it works or want to contribute, you're invited to explore the project!&lt;/p&gt;

&lt;p&gt;The idea for this project came from the desire to streamline web development. I noticed that writing HTML and CSS can be tedious and repetitive, so I decided to create a custom language that makes this process more intuitive.&lt;/p&gt;

&lt;p&gt;How My Language Works: "The language I created allows users to define the structure and style of a web page using a simple syntax. For example, instead of writing complex HTML and CSS, you can use a structure like this:  &lt;code&gt;page {&lt;br&gt;
    title: "My Enhanced Website";&lt;br&gt;
    header {&lt;br&gt;
        text: "Welcome!";&lt;br&gt;
        style: {&lt;br&gt;
            color: red;&lt;br&gt;
            font-size: 30px;&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
}&lt;/code&gt; &lt;br&gt;
This is then automatically transformed into a fully functional HTML page by the parser I built in Python.&lt;/p&gt;

&lt;p&gt;I implemented this project using Python, along with regular expressions (regex) to parse the custom syntax. Here's a breakdown of the key components of the parser:&lt;/p&gt;

&lt;p&gt;The WebPage class handles page elements such as headers, buttons, forms, and tables.&lt;br&gt;
The parse_webcode function reads the custom .webcode files and generates valid HTML from them.&lt;br&gt;
Elements like forms, tables, and buttons are defined in a simpler way within .webcode files, and the parser generates their corresponding HTML automatically."&lt;br&gt;
How It All Comes Together: "Once the .webcode file is parsed, it generates an output.html file that can be opened in a browser to see the result. Here's an example of a final output generated by the parser."&lt;/p&gt;

&lt;p&gt;Challenges and Future Plans: Throughout this project, one of the main challenges was ensuring that the parser correctly handles all HTML elements and attributes. Moving forward, I plan to expand the functionality by adding more interactive elements like JavaScript support and CSS frameworks for styling.&lt;/p&gt;

&lt;p&gt;Invitation to Collaborate: If you're interested in contributing or have suggestions for improvements, you're welcome to explore the project and leave your feedback. Here's the link to the repository where you can contribute.&lt;/p&gt;

&lt;p&gt;Snippet: The Custom Syntax in .webcode File&lt;br&gt;
Explain how your language allows users to write web elements in a simpler syntax: `page {&lt;br&gt;
    title: "My Enhanced Website";&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;header {
    text: "Welcome!";
    style: {
        color: red;
        font-size: 30px;
    }
}

button {
    text: "Click me";
    action: onClick {
        alert('Button clicked!');
    }
}

table {
    rows: [
        ["Name", "Age", "Email"],
        ["John", "30", "john@example.com"],
        ["Jane", "25", "jane@example.com"]
    ];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;}&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;Snippet: Python Parser Code to Convert the Syntax&lt;br&gt;
Here’s how your Python code reads the custom .webcode syntax and converts it into valid HTML. The parse_webcode function handles this process: `def parse_webcode(filename):&lt;br&gt;
    with open(filename, 'r') as file:&lt;br&gt;
        lines = file.read()&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Extract the page title
title_match = re.search(r'title:\s*"(.+?)";', lines)
title = title_match.group(1) if title_match else "Untitled Page"

# Create a new WebPage object
page = WebPage(title)

# Extract header, style, and other elements (like buttons and tables)
header_match = re.search(r'header\s*{\s*text:\s*"(.+?)";\s*style:\s*{(.+?)}\s*}', lines, re.DOTALL)
if header_match:
    header_text = header_match.group(1)
    style_text = header_match.group(2)
    style_dict = parse_style(style_text)
    page.add_header(header_text, style_dict)

# Extract table data
table_match = re.search(r'table\s*{\s*rows:\s*\[(.+?)\]\s*;\s*}', lines, re.DOTALL)
if table_match:
    table_rows = table_match.group(1).split("],")
    rows = [row.replace("[", "").replace("]", "").replace('"', '').split(",") for row in table_rows]
    page.add_table(rows)

return page.generate_html()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;`&lt;/p&gt;

&lt;p&gt;Snippet: Generated HTML Output&lt;br&gt;
This is an example of what the final HTML might look like after your custom syntax has been parsed and converted into HTML:  `&lt;br&gt;
&lt;/p&gt;My Enhanced Website&lt;br&gt;


&lt;h1&gt;Welcome!&lt;/h1&gt;

&lt;p&gt;Click me&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;Name&lt;/td&gt;
&lt;td&gt;Age&lt;/td&gt;
&lt;td&gt;Email&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;john@example.com&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Jane&lt;/td&gt;
&lt;td&gt;25&lt;/td&gt;
&lt;td&gt;jane@example.com&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
&lt;br&gt;
`&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>python</category>
      <category>html</category>
      <category>css</category>
    </item>
  </channel>
</rss>
