Help with Regular Expression to convert internal links

2012-08-26 Thread psilas
Hello,

I would like to convert Multimarkdown internal links to Pandoc ones,
and from reading my BBEdit manual it seems like it would be easiest to
use grep to accomplish this (that is if someone can help me with the
regular expression I would need).

Multimarkdown handles internal links like so:

[ Charging for School Activities][]

and I use these in my table of contents to link to the Header
"Charging for School Activities".

to do the same thing in I would to write

[ Charging for School Activities](#charging-for-school-activites)

After looking at how to use grep I realised I could spend a long time
trying to get this right.  I have a long table of contents in a number
of documents.

Can anyone help please?

Thanks in advance.

Simon.

-- 
-- 
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 bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at

If you have a feature request or would like to report a problem, 
please email "supp...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: 





Re: Help with Regular Expression to convert internal links

2012-08-26 Thread Kendall Conrad
Using just grep alone I don't see a way to fully do this. The closest I 
could come up with is:

find: \[ ([^\]]+)\]\[\]
Replace: [ \1](#\L\1)

This will turn
[ Charging for School Activities][]
into
[ Charging for School Activities](#charging for school activities)

This still leaves the spaces that need translating into dashes in anchor 
portion. If you're not afraid of using Perl, you can use a unix filter.

---
#!/usr/bin/perl
while (<>) {
if ($_ =~ m/\[ ([^\]]+)\]\[\](.*)$/) {
($n2=$1) =~ tr/A-Z /a-z\-/;
print "[ $1](#$n2)$2\n";
}
else {
print "$_";
}
}
---
You can run this from the #! menu.

-Kendall



On Sunday, August 26, 2012 11:36:05 AM UTC-4, psilas wrote:
>
> Hello, 
>
> I would like to convert Multimarkdown internal links to Pandoc ones, 
> and from reading my BBEdit manual it seems like it would be easiest to 
> use grep to accomplish this (that is if someone can help me with the 
> regular expression I would need). 
>
> Multimarkdown handles internal links like so: 
>
> [ Charging for School Activities][] 
>
> and I use these in my table of contents to link to the Header 
> "Charging for School Activities". 
>
> to do the same thing in I would to write 
>
> [ Charging for School Activities](#charging-for-school-activites) 
>
> After looking at how to use grep I realised I could spend a long time 
> trying to get this right.  I have a long table of contents in a number 
> of documents. 
>
> Can anyone help please? 
>
> Thanks in advance. 
>
> Simon. 
>

-- 
-- 
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 bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at

If you have a feature request or would like to report a problem, 
please email "supp...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: 





Re: Help with Regular Expression to convert internal links

2012-08-26 Thread Christopher Stone
On Aug 26, 2012, at 10:36, psilas  wrote:
> I would like to convert Multimarkdown internal links to Pandoc ones
__

This is pretty quick and dirty, but it works.

Kendall's Perl is prettier though.  :)

-ccs


on fndRepl(_find, _repl, _data)
  tell application "BBEdit"
set tocItemNew to replace _find using _repl searchingString _data options 
{search mode:grep, case sensitive:false, returning results:true}
  end tell
end fndRepl

try
  
  tell application "BBEdit"
set fRec to {found:true}
tell text of front text window
  select insertion point before character 1
  repeat while found of fRec = true
set fRec to find "^\\[.+\\]\\[\\]" options {backwards:false, case 
sensitive:false, search mode:grep, wrap around:false} with selecting match
if found of fRec = true then
  set _found to (found object of fRec) as text
  set AppleScript's text item delimiters to {"[", "]"}
  set tocItem to text item 2 of _found
  set tocItem to fndRepl("(.)", "\\l\\1", tocItem) of me
  set tocItemNew to "(#" & fndRepl(" ", "-", tocItem) of me & ")"
  set replText to fndRepl("(^\\[.+?\\])(\\[\\])", "\\1" & tocItemNew, 
_found) of me
  set text of selection to replText
end if
  end repeat
end tell
  end tell
  
on error eMsg number eNum
  set {c, s} to {return, "--"}
  set e to s & c & "Error: " & eMsg & c & s & c & "Error Number: " & eNum & c & 
s
  beep
  set dDlg to display dialog e buttons {"Cancel", "Copy", "OK"} default button 
"OK"
  if button returned of dDlg = "Copy" then
set the clipboard to e
  end if
end try


-- 
-- 
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 bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at

If you have a feature request or would like to report a problem, 
please email "supp...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: 





Re: Help with Regular Expression to convert internal links

2012-08-26 Thread psilas
Thanks very much for this Kendall, I very much appreciate it.  I managed to 
get to the same step as yourself with Grep in the end but couldn't manage 
the dashes part. 

My grep pattern is practically the same as your own:

Find:
\[(.*)\][[]]

Replace:
[\1](#\L\1\E)

I struggled a little to get the perl script going but fortunately 
Christoper Stone emailed me an Apple Script he wrote that also works well 
and I simply put in the scripts folder of BBEdit.  Thanks again.  I am 
astonished at how much help was offered to me in so short a time!

Simon.

On Sunday, August 26, 2012 8:35:22 PM UTC+1, Kendall Conrad wrote:
>
> Using just grep alone I don't see a way to fully do this. The closest I 
> could come up with is:
>
> find: \[ ([^\]]+)\]\[\]
> Replace: [ \1](#\L\1)
>
> This will turn
> [ Charging for School Activities][]
> into
> [ Charging for School Activities](#charging for school activities)
>
> This still leaves the spaces that need translating into dashes in anchor 
> portion. If you're not afraid of using Perl, you can use a unix filter.
>
> ---
> #!/usr/bin/perl
> while (<>) {
> if ($_ =~ m/\[ ([^\]]+)\]\[\](.*)$/) {
> ($n2=$1) =~ tr/A-Z /a-z\-/;
> print "[ $1](#$n2)$2\n";
> }
> else {
> print "$_";
> }
> }
> ---
> You can run this from the #! menu.
>
> -Kendall
>
>
>
> On Sunday, August 26, 2012 11:36:05 AM UTC-4, psilas wrote:
>>
>> Hello, 
>>
>> I would like to convert Multimarkdown internal links to Pandoc ones, 
>> and from reading my BBEdit manual it seems like it would be easiest to 
>> use grep to accomplish this (that is if someone can help me with the 
>> regular expression I would need). 
>>
>> Multimarkdown handles internal links like so: 
>>
>> [ Charging for School Activities][] 
>>
>> and I use these in my table of contents to link to the Header 
>> "Charging for School Activities". 
>>
>> to do the same thing in I would to write 
>>
>> [ Charging for School Activities](#charging-for-school-activites) 
>>
>> After looking at how to use grep I realised I could spend a long time 
>> trying to get this right.  I have a long table of contents in a number 
>> of documents. 
>>
>> Can anyone help please? 
>>
>> Thanks in advance. 
>>
>> Simon. 
>>
>

-- 
-- 
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 bbedit@googlegroups.com
To unsubscribe from this group, send email to
bbedit+unsubscr...@googlegroups.com
For more options, visit this group at

If you have a feature request or would like to report a problem, 
please email "supp...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: