Last active
November 19, 2017 12:49
-
-
Save opn/a347f393280d81035b5c1abb3b34183d to your computer and use it in GitHub Desktop.
This is Revigniter controller 'onetab.lc' from the domain 'rest.livecode.world'.
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
<?lc | |
/* = DESCRIPTION = | |
-- | |
The onetab.lc controller can be found at: onetab.lc | |
Here is some test help | |
Below we declare the global variables gControllerHandlers AND gData | |
And put the public method names that we wish to expose into gControllerHandlers | |
-- | |
*/ | |
global gControllerHandlers, gData | |
put "htmlParagraphs,index,html,markdown,onetab" into gControllerHandlers | |
/* == INITIALISATION == | |
-- | |
Put all the handlers and variables that you wish to merge into your views | |
into the global array "gData". | |
Write any initialisation statements (loading any neeeded libraries for instance) | |
into the initialisation handler "onetab.lc" below. | |
-- | |
*/ | |
command onetab | |
start using stack (gAPPPATH & "stacks/lib_Fedwiki.livecode") | |
end onetab | |
/* == CODE == | |
-- | |
Here we put the public handlers the controller uses and exposes as urls. | |
-- | |
*/ | |
on index | |
put fedwiki_GetDroppedUrl ($_POST_RAW) into someUrl | |
-- https://www.one-tab.com/page/VecNztSjSAuL_u_UiaXMjw | |
if someUrl begins with "https://www.one-tab.com" then | |
put onetab_FetchPageArray (someUrl) into pageArray | |
put json_FromArray (pageArray) into someJSON | |
else | |
put "Not a [[OneTab]] web page. Visit OneTab to start collaborative bookmarking - [https://www.one-tab.com/ one-tab.com]" into someInfo | |
put fedwiki_ConstructErrorJSON (someInfo, "Error") into someJSON | |
end if | |
fedwiki_ReturnJson someJSON | |
end index | |
on html | |
put fedwiki_GetDroppedUrl ($_POST_RAW) into someUrl | |
if someUrl begins with "https://www.one-tab.com" then | |
put onetab_FetchPageArray (someUrl, "htmlSection") into pageArray | |
put json_FromArray (pageArray) into someJSON | |
else | |
put "Not a [[OneTab]] html page. Visit OneTab to start collaborative bookmarking - [https://www.one-tab.com/ one-tab.com]" into someInfo | |
put fedwiki_ConstructErrorJSON (someInfo, "Error") into someJSON | |
end if | |
fedwiki_ReturnJson someJSON | |
end html | |
on htmlParagraphs | |
put fedwiki_GetDroppedUrl ($_POST_RAW) into someUrl | |
if someUrl begins with "https://www.one-tab.com" then | |
put onetab_FetchPageArray (someUrl, "htmlParagraphs") into pageArray | |
put json_FromArray (pageArray) into someJSON | |
else | |
put "Not a [[OneTab]] html page. Visit OneTab to start collaborative bookmarking - [https://www.one-tab.com/ one-tab.com]" into someInfo | |
put fedwiki_ConstructErrorJSON (someInfo, "Error") into someJSON | |
end if | |
fedwiki_ReturnJson someJSON | |
end htmlParagraphs | |
on markdown | |
put fedwiki_GetDroppedUrl ($_POST_RAW) into someUrl | |
-- https://www.one-tab.com/page/VecNztSjSAuL_u_UiaXMjw | |
if someUrl begins with "https://www.one-tab.com" then | |
put onetab_FetchPageArray (someUrl, "markdown") into pageArray | |
put json_FromArray (pageArray) into someJSON | |
else | |
put "Not a [[OneTab]] web page. Visit OneTab to start collaborative bookmarking - [https://www.one-tab.com/ one-tab.com]" into someInfo | |
put fedwiki_ConstructErrorJSON (someInfo, "Error") into someJSON | |
end if | |
fedwiki_ReturnJson someJSON | |
end markdown | |
function dep_ListShortFiles | |
put "lib_Fedwiki.livecode" into shortFiles | |
return shortFiles | |
end dep_ListShortFiles | |
private function onetab_FetchPageArray oneTabUrl, pType | |
put url oneTabUrl into someHtml | |
-- | |
put "Below are a collection of links imported from [[OneTab]]. You can view these links here - [" & oneTabUrl && "one-tab.com]:" into headerBit | |
_ParseOneTabHtml someHtml, headerBit, markDownLinks, divSection, tabTitle | |
put the result into pageArray | |
-- | |
switch pType | |
case "htmlParagraphs" | |
_AddHtmlParagraphs pageArray, tabTitle, divSection, someMarkDown | |
break | |
case "htmlSection" | |
_AddHtmlSection pageArray, tabTitle, headerBit, divSection | |
break | |
case "markdown" | |
_AddMarkdownSection pageArray, tabTitle, markDownLinks | |
break | |
default | |
_AddHtmlParagraphs pageArray, tabTitle, divSection, someMarkDown | |
_AddHtmlSection pageArray, tabTitle, headerBit, divSection | |
_AddMarkdownSection pageArray, tabTitle, markDownLinks | |
end switch | |
fedwiki_CleanJournalArray pageArray | |
return pageArray | |
end onetab_FetchPageArray | |
private command _ParseOneTabHtml someHtml, headerBit, @markDownLinks, @divSection, @tabTitle | |
html_Tidy someHtml | |
put revXMLCreateTree (someHTML, false, true, false) into treeID | |
-- | |
put revXMLRootNode (treeID) into rootNode | |
put word 1 to -1 of revXMLNodeContents (treeID, "/html/head/title") into htmlTitle | |
put word 1 to -1 of revXMLNodeContents (treeID, "/html/body/div[1]/div[6]") into tabTitle | |
-- | |
put fedwiki_ConstructNewPageArray (tabTitle, headerBit) into pageArray | |
-- | |
put "vertical-align: middle; width:16px; height:16px" into faviconStyle | |
put "vertical-align: middle; padding-left: 10px; padding-right: 12px; text-decoration: none;" into someLinkstyle | |
put "<img src='[[faviconURL]]' style='[[faviconStyle]]' /><a target=_blank style='[[someLinkstyle]]' href='[[someTextURL]]'>[[someTextLink]]</a>" into linkTemplate | |
put "<div style='padding-left: 24px; padding-top: 8px; position: relative; font-size: 13px;'>" into divStart | |
-- | |
put revXMLChildNames (treeID, "/html/body/div[1]", CR, "div", true) into tNames | |
repeat with divNum = 7 to (the number of lines of tNames - 1) | |
put "/html/body/div[1]/div[" & divNum & "]" into someNode | |
put someNode & "/img" into imgNode | |
put revXMLAttribute (treeID, imgNode, "src") into faviconURL | |
put someNode & "/a" into aNode | |
put revXMLAttribute (treeID, aNode, "href") into someTextURL | |
put revXMLNodeContents (treeID, aNode) into someTextLink | |
replace CR with space in someTextLink | |
-- | |
set the itemdelimiter to slash | |
put item 3 of someTextURL into tldBit | |
set the itemdelimiter to "." | |
put item -2 to -1 of tldBit into tldBit | |
set the itemdelimiter to "," | |
-- construct markDownLinks | |
if the number of chars of someTextLink > 30 then | |
get char 1 to 30 of someTextLink | |
put word 1 to -2 of it into truncatedLink -- using token seems to break the encoding for json_FromArray??? | |
put "..." after truncatedLink | |
put "-" && truncatedLink && "- [" & someTextURL && tldBit & "]" into markDownLink | |
else | |
put "-" && someTextLink && "- [" & someTextURL && tldBit & "]" into markDownLink | |
end if | |
put CR & markDownLink after markDownLinks | |
-- construct divSection | |
put merge (linkTemplate) into htmlLine | |
put divStart & htmlLine & "</div>" into divBit | |
put divBit & CR after divSection | |
end repeat | |
-- | |
revDeleteXMLTree treeID | |
return pageArray | |
end _ParseOneTabHtml | |
private command _AddMarkdownSection @pageArray, tabTitle, markDownLinks | |
put "#" && tabTitle & CR & markDownLinks into someMarkDown | |
fedwiki_AddMarkdownToPageArray pageArray, someMarkDown | |
end _AddMarkdownSection | |
private command _AddHtmlParagraphs @pageArray, tabTitle, divSection, someMarkDown | |
put "#" && tabTitle into someMarkDown | |
fedwiki_AddMarkdownToPageArray pageArray, someMarkDown | |
repeat for each line htmlLine in divSection | |
set the itemdelimiter to ">" | |
delete item 1 of htmlLine | |
set the itemdelimiter to "<" | |
delete item -1 of htmlLine | |
fedwiki_AddHtmlToPageArray pageArray, htmlLine | |
end repeat | |
end _AddHtmlParagraphs | |
private command _AddHtmlSection @pageArray, tabTitle, headerBit, divSection | |
put "<h3>" & tabTitle & "</h3>" & CR into divHeader | |
put headerBit & "<p>" & CR after divHeader | |
put divHeader before divSection | |
fedwiki_AddHtmlToPageArray pageArray, divSection | |
end _AddHtmlSection | |
command html_Tidy @someHtml | |
put the tempname & ".html" into inputFile | |
put someHtml into url ("file:" & inputFile) | |
put "tidy --show-info false --show-warnings false --force-output true --char-encoding utf8 --input-encoding utf8 --output-encoding utf8 --output-xml true -asxml -m" && inputFile into someShell | |
-- put "tidy -q -utf8 -asxml -m" && inputFile into someShell | |
put shell (someShell) into shellResult | |
put url ("file:" & inputFile) into someHtml | |
end html_Tidy | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment