Skip to content

Commit

Permalink
Merge pull request #44 from KxSystems/codekxv2
Browse files Browse the repository at this point in the history
Help uses v2 of code.kx.com
  • Loading branch information
awilson-kx authored Apr 16, 2019
2 parents 6981011 + 4a9fe6a commit aab4cab
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions jupyterq_help.q
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@
\l jupyterq_htmlgen.q
/ find the link and title for a name as string or symbol
p)def< findkw(soup,kw):
tag=soup.find('a',text=kw)
okw=kw
if not isinstance(kw,str): return None
kw = kw[3:] if '.q.' == kw[:3] else '' if kw == '.q' else kw
if not len(kw): return okw,'Reference card',''
namespace=False
if '.'==kw[0] and 2==len(kw): # might be a namespace
namespace=True
tag=soup.find('a',attrs={'class':'headerlink','href':'#'+kw[1:].lower()})
else:
tag=soup.find('a',text=kw)
if tag==None:
return tag
href=tag.get('href',default='https://code.kx.com')
title=tag.get('title',default=kw)
return tag
href=tag.get('href',default='https://code.kx.com/v2/ref/')
title= tag.parent.fetchNextSiblings('p')[0].text if namespace else tag.get('title',default=kw)
#title might be peer to the tag
if title==kw:
links=soup.findAll('a',attrs={'href':href})
for link in links:
if link.get_text()!=kw:
title=link.get_text()
return kw,title,href
return okw,title,href
bs:.p.import[`bs4;`:BeautifulSoup;>]
refcard:@[req:{"c"$.p.import[`urllib.request][`:urlopen][x][`:read][]`};"https://code.kx.com/q/ref/card/";{0}];
refcard:@[req:{"c"$.p.import[`urllib.request][`:urlopen][x][`:read][]`};(hb:"https://code.kx.com/v2/"),"ref";{0}];
offline:0~refcard;
if[not offline;findkw:findkw[bs[refcard;`html.parser];]];
find:{$[offline&kw:(x:`$sstring x)in qkw;"Sorry no help is available, as the kernel did not have access to code.kx.com when it was started";kw;findkw x;0]}
Expand All @@ -33,27 +42,34 @@ mb:{ / x can be string or keyword, title and link to code.kx.com
:mt'[`plain`html]!("\n\n"sv x;html)}
mbud:{[x;v]
tc:csv sv ("type: ";"count: "),'-3!'(type;count)@\:v;
l:"https://code.kx.com/q/ref/datatypes/";
l:hb,"basics/datatypes/";
html:hg.div hg.h4[sstring x],raze hg.p each(tc;hg.pre hg.code[(1#`class)!1#`q;.Q.s v];hg.a[`href`target!(l;"_blank");"Datatypes"]);
text:"\n\n"sv (sstring x;tc;.Q.s v);
mt'[`plain`html]!(text;html)}
qkw:.Q.res,key`.q;
/ add .z manually, can't find a reference variable for these
mbpy:{[x;v;ph]
tc:("embedPy";"foreign")[.p.i.isf v];
l:hb,"ml/embedPy";
html:hg.div hg.h4[sstring x],raze hg.p each(tc," object, help for embedPy is ",hg.a[`href`target!(l;"_blank");"here"];hg.pre hg.code ph);
text:"\n\n" sv(sstring x;tc," object, for help with embedPy go to: ",l;ph);
mt'[`plain`html]!(text;html)}
qkw:.Q.res,key`.q;qkw,:` sv'`.q,'key`.q / keywords and .q.contents
/ add .z manually, can't find a neference variable for these
qkw,:` sv'`.z,'`a`ac`b`bm`c`d`D`e`exit`f`h`i`k`K`l`n`N`o`p`P`pc`pd`pg`ph`pi`pm`po`pp`ps`pw`q`s`t`T`ts`u`vs`w`W`wc`wo`ws`x`X`z`Z`zd
qkw,:` sv'`.Q,'key .Q
qkw,:` sv'`.h,'key .h

qkw,:`.j.k`.j.j
qkw,:`.q`.z`.Q`.h`.j / namespaces
qkw@:where not null qkw
help:{
/ hack as .z.t/T/d/D have different links
if[u:(`$sstring ox:x)in` sv'`.z,'`d`D`t`T;x:"time/date shortcuts"];
if[u:(`$sstring ox:x)in` sv'`.z,'`d`D`t`T;x:".z.T"]; //"time/date shortcuts"];
$[10=t:type info:find x;:mb info;0=t;; / keyword and offline (return text error) or online continue
first v:@[(0;)@value@;x;(1;)];:mb"Sorry no help available for:\n\n ",sstring x; / something unknown
"no help available"~ph:@[value;`.p.helpstr;{{""}}]v 1;:mbud[x;v 1]; / user defined, but not embedPy or foreign python object
:mb ph]; / python/embedPy object
if[u;info[0]:sstring ox]; / replace the original name if .z.t/T/d/D
if[info[2]like"http*";:mb info]; / in case links returned are absolute
if[".."~2#info 2;:mb("";"";"https://code.kx.com/q/ref/card/"),'info];
:mb"Sorry no help available for:\n\n",sstring x; / default
:mbpy[x;v 1]ph]; / python/embedPy object
if[u;info[0]:sstring x:ox]; / replace the original name if .z.t/T/d/D
if[info[2]like"http*";:mb info]; / in case links returned are absolute
:mb("";"";hb,"ref/"),'info;
}


Expand Down

0 comments on commit aab4cab

Please sign in to comment.