Skip to content

Commit

Permalink
Merge pull request #1 from PhoenixMachina/v0.5
Browse files Browse the repository at this point in the history
V0.5
  • Loading branch information
EtoileFilante committed Feb 24, 2016
2 parents c562be5 + 99b0bad commit ea8345c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 23 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ In your code, you need to have wherever you want to use it :
using TlalocTemplate
```

You need to create a tlaloc object and set the path to your config file :
You need to create a Tlaloc object and set the path to your config file :
```
tlaloc = TlalocEngine("path/to/conf.ini")
```

Inside your conf.ini, you need to have :
```
viewPath=pathWithYourViews
TemplatePath=pathWithYourTemplates
ResourcePath=pathWithYourResources
templatePath=pathWithYourTemplates
resourcePath=pathWithYourResources
```
By resources, we mean like css, javascript, all that stuff. Doesn't matter if they're in a subfolder.

Expand All @@ -44,5 +44,12 @@ addArg(mypage,name,value)

Here's a look at what your view file could look like :
```
${extends "header.html"}
Hey to you my friend ${username}! What's up?
```

##Render
Obviously you will want at some point to render what you've done. It's very easy :
```
render(mypage)
```
43 changes: 30 additions & 13 deletions src/Tlaloc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,29 @@ keywords = ["extends","for","endfor","addResource"] #Not all implemented yet
#Type Tlaloc
type TlalocEngine
viewPath::ASCIIString # path to views
TemplatePath::ASCIIString # path to templates
ResourcePath::ASCIIString # path to resources
templatePath::ASCIIString # path to templates
resourcePath::ASCIIString # path to resources
#Constructor
function TlalocEngine(path::ASCIIString="")
if path != ""
conf = ConfParse(path)
parse_conf!(conf)
viewPath = retrieve(conf, "default", "viewPath")
TemplatePath = retrieve(conf, "default", "TemplatePath")
ResourcePath = retrieve(conf, "default", "ResourcePath")
templatePath = retrieve(conf, "default", "templatePath")
resourcePath = retrieve(conf, "default", "resourcePath")

if((viewPath[end-1:end] != "/") && (viewPath[end-1:end] != "\\"))
viewPath = string(viewPath,"/")
end
if((templatePath[end-1:end] != "/") && (templatePath[end-1:end] != "\\"))
templatePath = string(templatePath,"/")
end
if((resourcePath[end-1:end] != "/") && (resourcePath[end-1:end] != "\\"))
resourcePath = string(resourcePath,"/")
end

end
new(viewPath, TemplatePath, ResourcePath)
new(viewPath, templatePath, resourcePath)
end
end

Expand All @@ -44,22 +55,28 @@ end
function parseView(page::Page)
response = open(readall, page.tlaloc.viewPath * page.view)
difference = 0 # We need this because eachMatch collects all the match and then treats them, which means the data concerning indexes starting from the second match needs to be adjusted
for match in eachmatch(r"\$\{([a-zA-Z0-9_ ]+)\}",response)

for amatch in eachmatch(r"\$\{([a-zA-Z0-9_ .\"]+)\}",response)
for keyword in keywords
reg_string = "$(keyword)"
reg = Regex(reg_string)
if ismatch(reg,match.match)
if ismatch(reg,amatch.match)
if keyword == "extends"
# Soon
if ismatch(Regex("extends \"([a-zA-Z0-9_. ]+)\""),amatch.match)
statement = match(Regex("\"([a-zA-Z0-9_. ]+)\""),amatch.match)
content = open(readall,page.tlaloc.templatePath * (statement.match)[2:end-1])
response = string(response[1:(amatch.offset)-1 + difference],content,response[((amatch.offset)+difference+(length(amatch.match))):end] )
difference = difference + length(content) - length(amatch.match)
end
elseif keyword == "addResource"
#Soon
end
end
end

if haskey(page.args,(match.match)[3:end-1])
var = (page.args)[(match.match)[3:end-1]]
response = string(response[1:(match.offset)-1 + difference],var,response[((match.offset)+difference+(length(match.match))):end] )
difference = difference + length(var) - length(match.match)
if haskey(page.args,(amatch.match)[3:end-1])
var = (page.args)[(amatch.match)[3:end-1]]
response = string(response[1:(amatch.offset)-1 + difference],var,response[((amatch.offset)+difference+(length(amatch.match))):end] )
difference = difference + length(var) - length(amatch.match)
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/apage.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
${extends} ${something} Hello ${name}, great to meet you!
${extends "testHeader.html"} ${something} Hello ${name}, great to meet you!
9 changes: 5 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ using Tlaloc
# Testing constructor
engine = TlalocEngine(string(dirname(Base.source_path()),"/test_conf.ini"))
@test typeof(engine) == TlalocEngine
@test engine.viewPath == "thisIsTheViewPath"
@test engine.TemplatePath == "thisIsTheTemplatePath"
@test engine.ResourcePath == "thisIsTheResource"
@test engine.viewPath == "thisIsTheViewPath/"
@test engine.templatePath == "thisIsTheTemplatePath/"
@test engine.resourcePath == "thisIsTheResource/"

#Testing Page constructor
aPage = Page(engine,"apage.html",Dict())
Expand All @@ -20,4 +20,5 @@ addArg(aPage,"name","aValue")

#Testing view
aPage.tlaloc.viewPath = string(dirname(Base.source_path()),"/")
@test render(aPage) == "\${extends} \${something} Hello aValue, great to meet you!\n"
aPage.tlaloc.templatePath = string(dirname(Base.source_path()),"/")
@test render(aPage) == "Yop\n\nBest header ever\n \${something} Hello aValue, great to meet you!\n"
3 changes: 3 additions & 0 deletions test/testHeader.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Yop

Best header ever
4 changes: 2 additions & 2 deletions test/test_conf.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
viewPath=thisIsTheViewPath
TemplatePath=thisIsTheTemplatePath
ResourcePath=thisIsTheResource
templatePath=thisIsTheTemplatePath
resourcePath=thisIsTheResource

0 comments on commit ea8345c

Please sign in to comment.