Skip to content

Instantly share code, notes, and snippets.

@opn
Last active November 12, 2017 13:30
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 opn/441104b560483ed4a8546c005d5f9e18 to your computer and use it in GitHub Desktop.
Save opn/441104b560483ed4a8546c005d5f9e18 to your computer and use it in GitHub Desktop.
This the is Revigniter controller 'fedwiki.lc' from the domain 'rest.livecode.world'.
<?lc
/* = DESCRIPTION =
--
The fedwiki.lc controller can be found at: fedwiki.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 "clean_Title,clean_FirstLetter,fix_Title,compact_Refs,fork_Cleaner,index,journal,fedwiki" 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 "fedwiki.lc" below.
--
*/
command fedwiki
start using stack (gAPPPATH & "stacks/lib_Fedwiki.livecode")
end fedwiki
/* == CODE ==
--
Here we put the public handlers the controller uses and exposes as urls.
--
*/
on index
put fedwiki_GetDroppedUrl ($_POST_RAW) into droppedURL
put fedwiki_ConstructErrorJSON (droppedURL &CR&CR& $_POST_RAW) into errorJSON
fedwiki_ReturnJSON errorJSON
end index
function dep_ListShortFiles
put "lib_Fedwiki.livecode" into shortFiles
return shortFiles
end dep_ListShortFiles
function fedwiki_FetchSinglePageArray droppedURL
-- http://tools.progressivedemocracy.cc/collaborative-bookmarking.html
set the itemdelimiter to slash
if the number of items of droppedURL is not 4 then return "Error, dropped url is not a single .html wiki page"
set the itemdelimiter to "."
if item -1 of droppedURL is not "html" then return "Error, dropped url is not a single .html wiki page"
-- http://tools.progressivedemocracy.cc/collaborative-bookmarking.json?random=1b93eacd
put droppedURL into jsonURL
put "json" into item -1 of jsonURL
put url jsonURL into someJSON
put json_ToArray (someJson) into pageArray
put pageArray ["story"] into storyArray
if storyArray is not an array then
return empty
end if
return pageArray
end fedwiki_FetchSinglePageArray
on compact_Refs
put fedwiki_GetDroppedUrl ($_POST_RAW) into droppedURL
--
put fedwiki_FetchSinglePageArray (droppedURL) into pageArray
put pageArray ["title"] into pageTitle
put pageArray ["story"] into storyArray
if storyArray is not an array then
put fedwiki_ConstructErrorJSON ("This is not a single wiki page. Only drop references to single pages on this transporter." &CR&CR& droppedURL) into errorJSON
fedwiki_ReturnJSON errorJSON
end if
--
put 1 into newItemNum
put item 2 of the extents of storyArray into maxItemNum
repeat with itemNum = 1 to maxItemNum
put storyArray [itemNum] into itemArray
put itemArray ["type"] into itemType
if itemType = "reference" then
put itemArray ["site"] into someSite
put itemArray ["slug"] into someSlug
put itemArray ["title"] into someTitle
put "- [[" & someTitle & "]]" & CR after markDownLinks
else
put itemArray into newStoryArray [ itemNum]
end if
end repeat
delete char -1 of markDownLinks
--
put "# See also" & CR & CR & markDownLinks into seeAlsoSection
put fedwiki_ConstructStoryMarkdownArray (seeAlsoSection) into seeAlsoItemArray
fedwiki_AddToEndOfIndexArray seeAlsoItemArray, newStoryArray
--
put newStoryArray into pageArray ["story"]
put json_FromArray (pageArray) into pageJSON
fedwiki_ReturnJSON pageJSON
end compact_Refs
on fork_Cleaner
put fedwiki_GetDroppedUrl ($_POST_RAW) into droppedURL
put fedwiki_CleanLastLineUpJournal (droppedURL, "create,fork", "fedwiki-transport") into cleanJSON
fedwiki_ReturnJSON cleanJSON
end fork_Cleaner
on journal
put fedwiki_GetDroppedUrl ($_POST_RAW) into droppedURL
put fedwiki_CleanLastLineUpJournal (droppedURL) into cleanJSON
fedwiki_ReturnJSON cleanJSON
end journal
on clean_Title
put fedwiki_GetDroppedUrl ($_POST_RAW) into droppedURL
put fedwiki_FetchLastLineUpArray (droppedURL) into pageArray
--
put pageArray ["Title"] into pageTitle
-- put toUpper (char 1 of pageTitle) into char 1 of pageTitle
put text_InitialCaps (pageTitle) into pageTitle
put pageTitle into pageArray ["Title"]
--
put json_FromArray (pageArray) into pageJSON
fedwiki_ReturnJSON pageJSON
end clean_Title
on fix_Title
put fedwiki_GetDroppedUrl ($_POST_RAW) into droppedURL
put fedwiki_FetchLastLineUpArray (droppedURL) into pageArray
if pageArray is false then
put merge ("The dropped url [[droppedURL]] is not a Fedwiki url.") into errorText
fedwiki_ExitAndReturnError errorText, "Not a Fedwiki URL"
end if
--
put pageArray ["Title"] into pageTitle
replace "-" with space in pageTitle
put word 1 to -1 of pageTitle into pageTitle
put text_InitialCaps (pageTitle) into pageTitle
put pageTitle into pageArray ["Title"]
--
put json_FromArray (pageArray) into pageJSON
fedwiki_ReturnJSON pageJSON
end fix_Title
on clean_FirstLetter
put fedwiki_GetDroppedUrl ($_POST_RAW) into droppedURL
put fedwiki_FetchLastLineUpArray (droppedURL) into pageArray
--
put pageArray ["Title"] into pageTitle
put toUpper (char 1 of pageTitle) into char 1 of pageTitle
put pageTitle into pageArray ["Title"]
--
put json_FromArray (pageArray) into pageJSON
fedwiki_ReturnJSON pageJSON
end clean_FirstLetter
function text_InitialCaps someText
repeat with wordNum = 1 to the number of words of someText
put tolower(word wordNum of someText) into someWord
put toupper(char 1 of someWord ) into char 1 of someWord
put someWord into word wordNum of someText
end repeat
return someText
end text_InitialCaps
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment