Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aminya/acuteml.jl

Acute Markup Language - HTML/XML in Julia
https://github.com/aminya/acuteml.jl

dom html julia templating-engine web-framework web-library xml xml-files xpath

Last synced: 17 days ago
JSON representation

Acute Markup Language - HTML/XML in Julia

Awesome Lists containing this project

README

        

# AcuteML
## Acute Markup Language

[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://aminya.github.io/AcuteML.jl/dev)
![CI](https://github.com/aminya/AcuteML.jl/workflows/CI/badge.svg)
[![Test Coverage](https://codecov.io/gh/aminya/AcuteML.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/aminya/AcuteML.jl)
![SnoopCompile](https://github.com/aminya/AcuteML.jl/workflows/SnoopCompile/badge.svg)

AcuteML is an Acute Markup Language (AML) for Web/XML development in Julia.

* It automatically creates or extracts HTML/XML files from Julia types!

* It also has a general templating engine, which can be used for any type of documents.

# Installation and Usage
```julia
using Pkg
Pkg.add("AcuteML")
```
```julia
using AcuteML
```

# Documentation
Click on the badge: [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://aminya.github.io/AcuteML.jl/dev)

See [Type Definition](https://aminya.github.io/AcuteML.jl/dev/#Main-macro-and-I/O-1) for a comprehensive introduction to syntax. You can use `@aml` macro to define a Julia type, and then the package automatically creates a xml or html associated with the defined type.

# Readme Content
- [Installation and Usage](#installation-and-usage)
- [Documentation](#documentation)
- [Example - Simple](#example---simple)
- [Example - Struct Definition](#example---struct-definition)
- [Example - Creator](#example---creator)
- [Example - Extractor](#example---extractor)
- [Templating](#templating)
- [Example - Template Rendering using Functions](#example---template-rendering-using-functions)
- [Example - Template Rendering using Files](#example---template-rendering-using-files)

# Example - Simple
```julia
using AcuteML

# the xml/html name of each property is written in front of it (e.g. "body")
# `~` means that the struct property name is the same as xml/html name

@aml mutable struct Body "body"
h1, "~"
p::Vector{String}, "~"
end

@aml mutable struct Page doc"html"
body::Body, "~"
end

b = Body(h1 = "My heading", p = ["Paragraph1", "Paragraph2"])
d = Page(body = b)
pprint(d)
```
```html
julia> pprint(d)


My heading


Paragraph1


Paragraph2


```

-----------------------------------------------
More advanced Examples are given in the following:

# Example - Struct Definition

First, we define the structs using `@aml` to store the data in:

```julia
using AcuteML

# Types definition

# Person Type
@aml mutable struct Person "person", check_course
age::UInt64, "~"
field, "study-field"
GPA::Float64 = 4.5, "~", GPAcheck
courses::Vector{String}, "taken-courses"
professors::UN{DataFrame} = nothing, "table"
id::Int64, att"~"
comment::UN{String} = nothing, txt"end"
end

@aml mutable struct University doc"university"
name, att"university-name"
people::Vector{Person}, "person"
end

```

```julia
# Value Checking Functions
GPAcheck(x) = x <= 4.5 && x >= 0

function check_course(age, field, GPA, courses, professors, id, comment)

if field == "Mechanical Engineering"
relevant = ["Artificial Intelligence", "Robotics", "Machine Design"]
elseif field == "Computer Engineering"
relevant = ["Julia", "Algorithms"]
else
error("study field is not known")
end

return any(in.(courses, Ref(relevant)))
end
```
-------------------------------------------------------

# Example - Creator

After we defined the structs, we can create instances of them by passing our data to the fields:

```julia

P1 = Person(age=24, field="Mechanical Engineering", courses = ["Artificial Intelligence", "Robotics"], id = 1, comment = "He is a genius")
P2 = Person(age=18, field="Computer Engineering", GPA=4, courses=["Julia"], id = 2)

U = University(name="Julia University", people=[P1, P2])

U.people[2].GPA=4.2 # mutability support after Doc creation

```

```julia
# An example that doesn't meet the criteria function for GPA because GPA is more than 4.5
P3 = Person(age=99, field="Macro Wizard", GPA=10, courses=["Julia Magic"], id = 3)
julia>
GPA doesn't meet criteria function
```

```html
julia> pprint(P1) # or print(P1.aml)

24
Mechanical Engineering
4.5
Artificial Intelligence
Robotics
He is a genius

julia> pprint(U) # or print(U.aml)


24
Mechanical Engineering
4.5
Artificial Intelligence
Robotics
He is a genius


18
Computer Engineering
4.2
Julia

```

P3 with Tables.jl type:
```julia
Profs1 = DataFrame(course = ["Artificial Intelligence", "Robotics"], professor = ["Prof. A", "Prof. B"] )

P3 = Person(age=24, field="Mechanical Engineering", courses = ["Artificial Intelligence", "Robotics"], professors= Profs1, id = 1)
```
```html
julia> pprint(P3)

24
Mechanical Engineering
4.5
Artificial Intelligence
Robotics

course
professor

String
String

Artificial Intelligence
Prof. A

Robotics
Prof. B

```
-------------------------------------------------------

# Example - Extractor

After we defined the structs, we can automatically extract and store the data in their fields:

```julia
using AcuteML

xml = parsexml("""


24
Mechanical Engineering
4.5
Artificial Intelligence
Robotics
He is a genius


18
Computer Engineering
4.2
Julia

""")

# extract University
U = University(xml) # StructName(xml) extracts the data and stores them in proper format

# Now you can access all of the data by calling the fieldnames

julia>U.name
"Julia University"

# extract Person
P1 = U.people[1]

julia>P1.age
24

julia>P1.field
Mechanical Engineering

julia>P1.GPA
4.5

julia>P1.courses
["Artificial Intelligence", "Robotics"]

julia>P1.id
1

julia> P1.comment
"He is a genius"
```

-------------------------------------------------------
# Templating
AcuteML also provides a templating engine if you want to use templates instead of creating the types.

-------------------------------------------------------

# Example - Template Rendering using Functions

This method only uses functions that return string. You can build your desired string and call the function for rendering.

```julia
## create person function to store out html template
newTemplate("person", :function)

function person(;id, age, field, GPA, courses)

# Build the taken courses section
loopOut=""
for course in courses
loopOut = loopOut * """ $(course) """
end

# Append all the sections and variables together
out = """

$(age)
$(field)
$(GPA)
$loopOut

"""

return out
end

# Call the function for rendering
out = person(
id = "1",
age = "24",
field = "Mechanical Engineering",
GPA = "4.5",
courses = ["Artificial Intelligence", "Robotics"]
)

print(out)

# you can also write the output to a file:
Base.write(filePath, out)
```

-------------------------------------------------------
# Example - Template Rendering using Files

You can render variables into html/xml files. However, you can't have multiline control flow Julia code in this method.

```julia
# you can create a file and edit the file directly by using
newTemplate("person")

# Add the following html code to the generated html file
#=

$(age)
$(field)
$(GPA)
$(courses[1])
$(courses[2])

=#

# Specify the template (or its path), and also the variables for rendering
out =render2file("person", false,
id = 1,
age = 24,
field = "Mechanical Engineering",
GPA = 4.5,
courses = ["Artificial Intelligence", "Robotics"])

# you pass `true` as the 2nd argument to overwrite person.html statically.
```