Locate a markdown document directly by html counterpart in browser

Hello!

This reveals the markdown document, based on the local html file currently rendered in your browser
this script goes into the WebpageHelper.app, see MacScripter / Script that automates the conversion from markdown to html and MacScripter / Locate and edit markdown counterpart of html file displayed in Safari for details.


property warnIcon : a reference to file ((path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle:Contents:Resources:AlertCautionIcon.icns")
-- © McUsr 2012 and put in Public Domain. You may not post,and not store this in a public accessible repository
-- but refer to the link here: http://macscripter.net/edit.php?id=154988
property main_property : {}

on run
	local myUnescapedUrl, mrkDownCounterPart
	try
		
		if main_property starts with "file" then
			if my isAvalidHtmlFileUrl(main_property) then
				
				set myUnescapedUrl to my decodefurl(main_property)
				
				set mrkDownCounterPart to findMarkdownCounterPart of me for myUnescapedUrl
				
				if mrkDownCounterPart is false then
					error "I find several markdown files, I can't resolve it" number 3001
				else if mrkDownCounterPart = null then
					error "I couldn't find any markdown file one level above or below the html file." number 3000
				else
					do shell script "open -R " & quoted form of (POSIX path of mrkDownCounterPart)
					return true
				end if
			else
				error "This script only works with local html files rendered in your browser" number 3000
			end if
		else
			error "This script only works with local html files rendered in your browser" number 3000
		end if
		
	on error e number n
		logMessage2("WebPageHelper", "RevealMarkdownFromBrowser -- Error: " & e & " " & n)
		tell application "SystemUIServer"
			activate
			display dialog e with title "Webpage Helper" buttons {"Ok"} default button 1 with icon warnIcon
		end tell
		if n = 3001 then
			do shell script "open -R " & quoted form of myUnescapedUrl
			return true
		else
			return false
		end if
	end try
end run

to findMarkdownCounterPart for ahtmlfile
	local pxFolder, nm, suf, parFol, counterPart, ct, markdowncounterpart
	set {pxFolder, nm, suf} to splitPxFile of me for ahtmlfile
	set parFol to parentFol of me for pxFolder
	set counterPart to ""
	
	set counterPart to paragraphs of (do shell script "find " & quoted form of parFol & " -name " & quoted form of (nm & ".text") & " -maxdepth 3 -print 2>/dev/null  || echo >/dev/null")
	set ct to length of counterPart
	if ct = 0 then
		return null
	else if ct > 1 then
		return false
	else -- if ct = 1 then
		set markdownlCounterPart to POSIX file counterPart as alias as text
		return markdownlCounterPart
	end if
end findMarkdownCounterPart

to splitPxFile for pxPath
	-- http://macscripter.net/viewtopic.php?id=39332
	local ppl, sl, s, tids, f
	set ppl to length of pxPath
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}
	
	set s to item -1 of text items of pxPath
	set sl to length of s
	if sl = ppl then
		set s to missing value
		set pxPath to text items 1 thru -1 of pxPath as text
	else
		set pxPath to text items 1 thru -2 of pxPath as text
	end if
	set AppleScript's text item delimiters to "/"
	
	set f to item -1 of text items of pxPath
	if f = "" then set f to missing value
	try
		set pxPath to (items 1 thru -2 of text items of pxPath)
		set pxPath to (pxPath as text) & "/"
	on error
		set pxPath to missing value
	end try
	set AppleScript's text item delimiters to tids
	return {pxPath, f, s}
end splitPxFile

to parentFol for aPxPath
	local tids, parFol
	set {tids, my text item delimiters} to {my text item delimiters, "/"}
	
	if (character (length of aPxPath) of aPxPath) = "/" then
		set parFol to text items 1 thru -3 of aPxPath as text
	else
		set parFol to text items 1 thru -2 of aPxPath as text
	end if
	set my text item delimiters to tids
	if parFol = "" then set parFol to "/"
	return parFol
end parentFol


on logMessage2(procName, Msg)
	-- needs a decl like this in the run handler (implicit or explicit )
	do shell script "/usr/bin/logger -s -t " & quoted form of procName & " " & quoted form of Msg
end logMessage2


on isAvalidHtmlFileUrl(theUrl)
	local ok, astid
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	if not text item 1 of theUrl is "file" then
		set AppleScript's text item delimiters to astid
		return false
	end if
	set AppleScript's text item delimiters to "."
	if not text item -1 of theUrl is "html" then
		set AppleScript's text item delimiters to astid
		return false
	end if
	
	set AppleScript's text item delimiters to astid
	return true
end isAvalidHtmlFileUrl

on decodefurl(anUrlFromABrowser)
	-- 27/08/12 Tested!
	-- konverterer escaped chars tilbake til normal 
	-- fjerner file, og local host. 
	-- localhost starter helt til å begynne med i tilfelle.
	local tmpUrl
	
	set tmpUrl to my rawURLDecode(anUrlFromABrowser)
	
	set tmpUrl to my str_replace({substringToReplace:"file://", replacementString:"", OriginalString:tmpUrl})
	
	if (offset of "localhost" in tmpUrl) is 1 then set tmpUrl to text 10 thru -1 of tmpUrl
	
	return tmpUrl
end decodefurl

--  DJ Bazzie Wazzie http://macscripter.net/edit.php?id=154949
on rawURLDecode(str)
	return do shell script "/bin/echo -n " & quoted form of str & " | php -r ' echo rawurldecode(fgets(STDIN)); '"
end rawURLDecode

on str_replace(R) -- Returns modified string
	-- R {substringToReplace: _ent, replacementString: _rent,OriginalString: _str}
	local _tids
	set _tids to AppleScript's text item delimiters
	set AppleScript's text item delimiters to R's substringToReplace
	set _res to text items of R's OriginalString as list
	set AppleScript's text item delimiters to R's replacementString
	set _res to items of _res as string
	set AppleScript's text item delimiters to _tids
	return _res
end str_replace