> How does one convert email and web addresses to hyperlinks using
> BBEdit.
What sort of input are you expecting? Just plain text, HTML, something else?
If it's plain text, there's an interesting URL for matching an email address:
http://daringfireball.net/2009/11/liberal_regex_for_matching_urls
There is a useful resource for regular expressions over here:
http://regexlib.com/REDetails.aspx?regexp_id=501
(the one I've linked to directly is for matching URIs)
The next bit is using BBEdit Find & Replace with grep to do the legwork :)
Hope this helps!
Alex
On Wed, May 18, 2011 at 08:38, Bo <bvhp...@gmail.com> wrote:
> How does one convert email and web addresses to hyperlinks using
> BBEdit.
>
I do it with the following Applescript. Caution: I'm not a scripting
expert. The script has worked well for me for several years, but you
should always check its output in case it does odd things for you.
================
-- This script turns selected text into a link.
-- Select text like www.google.com or http://www.google.com
-- or an email address like m...@example.com
-- and then run the script.
-- by Miraz Jordan, http://knowit.co.nz
tell application "BBEdit"
make new text document with properties {contents:selection} at
text window 1
tell front window
-- set the static bits of code
set h_ref to "<a href=\"http://"
set last_bracket to "\">"
set close_tag to "</a>"
set mail_to to "mailto:"
-- now we build the link
-- delete any superfluous angle brackets
replace "<" using ""
replace ">" using ""
select text 1
set selection to h_ref & selection & last_bracket &
selection & close_tag
-- clean up
-- lose a trailing slash on the link text
replace "/</a>" using "</a>"
-- delete an http:// on link text
replace ">http://" using ">"
-- add an http:// to the link if necessary
replace "href=\"www" using "href=\"http://www"
-- add a mailto: to the link if necessary
replace "href=\"(.+)@" using "href=\"mailto:\\1@" options {search
mode:grep, starting at top:true, wrap around:false, backwards:false,
case sensitive:false, match words:false, extend selection:false}
-- tidy
replace " </a>" using "</a>" options {search
mode:literal, starting
at top:true, wrap around:false, backwards:false, case sensitive:false,
match words:false, extend selection:false}
replace "href=\"http://http://" using "href=\"http://"
replace "href=\"mailto:http://" using "href=\"mailto:"
-- return the URL to the old window
set the_url to text 1
close active document saving no
set selection to the_url
select insertion point after selection
say "Here's your link."
end tell
end tell
================
Cheers,
Miraz
--
MacTip: Project your iPad2 - http://mactips.info/2011/05/project-your-ipad2
KnowIT: Get some send some: try out torrents -
http://knowit.co.nz/2011/05/get-some-send-some-try-out-torrents
Or are you looking for something even simpler?
email address: bbe...@googlegroups.com
email address as hyperlink: <a href="mailto:bbe...@googlegroups.com">mail bbedit group</a>
web address: http://groups.google.com/group/bbedit
web address as hyperlink: <a href="http://groups.google.com/group/bbedit">BBEdit Google Group</a>
If that answers your question in a way you were thinking, you may find these pages at W3C helpful. <http://www.w3schools.com/html/DEFAULT.asp>
bucky
I update a schedule for a client every few weeks, I paste the text into DreamWeaver, then I have to slog through turning all the email and web addresses to hyperlinks manually, bit of a nightmare.
Here is a sample schedule http://www.robertburridge.com/Workshops/schedule_sample.html that is basic html, next step would be to make the email and web addresses active hyperlinks.
It would be very nice to automate the process with a BBEdit grep, or a command line directive, what ever. I must have spent four hours on Google to no avail. Found an application http://www.unmarked.com/textsoap/ that stated it would do the trick but don't want to download it.
Any assistance would be much appreciated.
Thanks in advance.
> --
> You received this message because you are subscribed to the
> "BBEdit Talk" discussion group on Google Groups.
> To post to this group, send email to bbe...@googlegroups.com
> To unsubscribe from this group, send email to
> bbedit+un...@googlegroups.com
> For more options, visit this group at
> <http://groups.google.com/group/bbedit?hl=en>
> If you have a feature request or would like to report a problem,
> please email "sup...@barebones.com" rather than posting to the group.
> Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>
Bo von Hohenlohe
bvhp...@gmail.com
Untested and based on your sample. The regular expressions are bare minimums. bbedit can do global replacements with similar regular expressions but I typically find it better to work with the original file when I know a lot about it.
Your source has a bunch of <br> tags that will need some special attention that isn't here.
#!/usr/bin/perl
$PathToOriginal = "Workshops/schedule_sample.html";
$PathToReplacement = "Workshops/fixed_sample.html"
open INPUT, $PathToOriginal or die "Can't open $PathToOriginal\n";
open OUTPUT, ">$PathToReplacement" or die "Can't open $PathToReplacement\ for outpur\n";\
while (<INPUT>) # reads one line at a time
{
chomp; # removes the line end.
if (/(www.*)/)
{
$newline = "<a href= \" http://$1\"> $1 </a>\n";
print OUTPUT $newline;
next;
}
if (/(\S)@(\S)(*)/)
{
$newline = "<a href=\"mailto:$1@$2\"> $1@$2 </a> $3\n";
print OUTPUT $newline;
next;
}
print "$_\n"; # Just print the line if we haven't changed anything.
}
--
--> A fair tax is one that you pay but I don't <--
> Here is a sample schedule http://www.robertburridge.com/Workshops/schedule_sample.html that is basic html, next step would be to make the email and web addresses active hyperlinks.
Working with John Gruber's regex, I end up with something that looks like this:
Find: \b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))
Replace: <a href="\1">\1</a>
Tick the box for "Grep" and "Wrap around", click "Replace All":
> October 8-12, 2012<BR>
> <B>Loosen Up with Aquamedia Painting</B><BR>
> Vancouver Island Art Workshops<BR>
> Nanaimo, British Columbia,Canada<BR>
> Contact Mary Stewart, 250-716-1440<BR>
> marys...@telus.net<BR>
> <a href="http://www.VancouverIslandArtWorkshops.com">http://www.VancouverIslandArtWorkshops.com</a><BR>
> <BR>
> November 2-4, 2012<BR>
> <B>Create Today with Wild Abandon - No Brushes!</B><BR>
> 3-day Workshop (Friday-Sunday)<BR>
> San Luis Obispo Museum of Art, San Luis Obispo, CA<BR>
> Contact off...@sloartcenter.org (805) 543-8562 ext. 14<BR>
> <a href="www.sloartcenter.org">www.sloartcenter.org</a><BR>
> <BR>
Then of course I realised that John Gruber's regex is for URL detection. D'oh! So off to regexlib.com to find an email regex:
^([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$
This email regex is appropriated from http://regexlib.com/REDetails.aspx?regexp_id=88
So Find & Replace gets us this (note I've wrapped the entire expression in brackets and removed the ^ and $ since I'm trying to *detect* rather than *validate* email addresses):
Find: ((?:[\w\-\.]+)@(?:(\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4})))
Replace: <a href="email:\1">\1</a>
Tick the box for "Grep" and "Wrap around", click "Replace All":
> October 8-12, 2012<BR>
> <B>Loosen Up with Aquamedia Painting</B><BR>
> Vancouver Island Art Workshops<BR>
> Nanaimo, British Columbia,Canada<BR>
> Contact Mary Stewart, 250-716-1440<BR>
> <a href="email:marys...@telus.net">marys...@telus.net</a><BR>
> <a href="http://www.VancouverIslandArtWorkshops.com">http://www.VancouverIslandArtWorkshops.com</a><BR>
> <BR>
> November 2-4, 2012<BR>
> <B>Create Today with Wild Abandon - No Brushes!</B><BR>
> 3-day Workshop (Friday-Sunday)<BR>
> San Luis Obispo Museum of Art, San Luis Obispo, CA<BR>
> Contact <a href="email:off...@sloartcenter.org">off...@sloartcenter.org</a> (805) 543-8562 ext. 14<BR>
> <a href="www.sloartcenter.org">www.sloartcenter.org</a><BR>
> <BR>
As for automating this, if I have to start writing any kind of script at all I'll just do the whole thing in Perl (and use that Perl script as a filter in BBEdit). I'll leave it up to the AppleScript guys on the list to suggest a script that will automate this Find & Replace in BBEdit (searching the list archives might uncover something interesting).
You can get part way to automation by saving those Find/Replace dialog options - select "Save…" in the little "g" menu, to the left of the "Previous" button. I've saved them as "URL to HREF" and "Email to HREF" respectively. This reduces the process of replacing web sites and email addresses to:
- ⌘F (open the Find/Replace dialog)
- Select "URL to HREF" from "g" menu
- Click "Replace All"
- Select "Email to HREF" from "g" menu
- Click "Replace All"
Finally, remember that these regexes will only catch about 99% of the URLs. You'll still need to proof read the HTML page in a browser to make sure they worked properly!
Hope this helps
Alex
PS: If you're interested in learning how those regular expressions work and how to craft your own, I recommend Jeffrey E.F. Friedl, "Mastering Regular Expressions" - http://oreilly.com/catalog/9780596528126
On 18/05/2011, at 12:57 , Bo wrote:
> "URL to HREF"
> Find: \b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]
> \s]|/)))
> Replace: <a href="http://\1">\1</a>
> Replace: <a href="http://\1" target="_blank">\1</a> (add
> target="_blank")
You'll be better off without the 'http://' bit there, since the regex will match URLs that already start with a scheme. Thus you'll end up with "http://http://www.VancourverIslandArtWorkshop.com" as a URL in one of the items down towards the bottom of the sample report that you showed us. This correction is potentially job-saving, so I'm posting to the list.
To add the "http://" in there, I'd suggest a third regex, which you will need to run *last*:
Find: href\s*=\s*"(?![[:alnum:]/]*?:)
Replace: href="http://
Tick the box for "Grep" and "Wrap around".
This will add the "http://" bit to only those HREF URIs which do not have something that looks like a scheme specifier in front:
- href="mailto:j...@example.com" will not be touched
- href="http://foo.example.com" will not be touched
- href="foo.example.com" will become href="http://foo.example.com"
- foo.example.com by itself will not be touched
Alex
PS: I made a boo-boo. The correct scheme for emails is "mailto" not "email". That's the second "Email to HREF" regex I posted earlier.
______________________________________________________________________my $input = '';while (<>) {$input .= $_;}