Created
September 29, 2015 21:52
-
-
Save m-2k/18a438024bbe53b3b4dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(doc). | |
-compile(export_all). | |
-include_lib("kvs/include/feed.hrl"). | |
-include_lib("nitro/include/nitro.hrl"). | |
-include_lib("n2o/include/wf.hrl"). | |
main() -> #dtl{file="doc",app=review,bindings=[{body,body()}]}. | |
body() -> [ | |
#h2{body= "Docs search"}, | |
#textbox{ id=query }, | |
#button {body="Search",postback=search,source=[query]}, | |
#panel{id=results} | |
]. | |
event(search) -> wf:update(results,#panel{id=results,body=search(n2o,wf:q(query))}); | |
event(_) -> []. | |
re(Q) -> <<"(?:<h\\d[^>]*?\\ id=[\'\"](sec\\d{1,3})[\'\"][^>]*?>(.*?)<\\/h\\d>.*?)+",Q/binary,"[^>]+<">>. | |
search(App,Q) -> | |
lists:foldl(fun(Path,Acc) -> | |
{ok,Bin}=file:read_file(Path), | |
case re:run(Bin,re(Q),[unicode,global,{capture,[1,2],binary},dotall,caseless]) of | |
{match,Match} -> | |
Page=filename:basename(Path), | |
Forms=#panel{body=[ | |
#h5{body=Page}, | |
[ begin | |
Url=["http://synrc.com/apps/",wf:to_binary(App),"/doc/web/",Page,$#,Sec], | |
#panel{body=#link{body=T,href=Url,target="_blank"}} | |
end||[Sec,T] <- Match] | |
]}, | |
[Forms|Acc]; | |
nomatch -> Acc end | |
end,[],filelib:wildcard(code:lib_dir(App)++"/doc/*.htm")). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment