Skip to content

Instantly share code, notes, and snippets.

@m-2k
Created September 29, 2015 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-2k/18a438024bbe53b3b4dd to your computer and use it in GitHub Desktop.
Save m-2k/18a438024bbe53b3b4dd to your computer and use it in GitHub Desktop.
-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