2

Disclaimer: I got as far as:

cat GUIDE.md  | sed 's/{@link \([^#]*\)#\([^ ]*\)}/[\1#\2](https:\/\/mercmobily.github.io\/best-webdriver\/\1#\2/g'  | less

So, it's nearly functional.

I have a GUIDE.md file which is in pure markdown, but it includes JSDOC links.

I need to scan the GUIDE.md file and change these:

Remember that in {@link Browser#setAlwaysMatchKey AlwaysMatch} or {@link Browser#setRootKey}  (all of the {@link Browser Browser} class), you must try [...]

Into:

Remember that in [AlwaysMatch](https://mercmobily.github.io/best-webdriver/Browser#setAlwaysMatchKey) or [Browser#setRootKey](https://mercmobily.github.io/best-webdriver/Browser#setRootKey) (all of the [Browser](https://mercmobily.github.io/best-webdriver/Browser) class), you must try [...]

So basically:

{@link Class#method Description}
->
[Description](https://mercmobily.github.io/best-webdriver/Class.html#method)

{@link Class#method}
->
[Class#method](https://mercmobily.github.io/best-webdriver/Class.html#method)

Where #method is optional, so:

{@link Class Description}
->
[Description](https://mercmobily.github.io/best-webdriver/Class.html)

{@link Class}
->
[Class](https://mercmobily.github.io/best-webdriver/Class.html)

At this point, I got up to:

cat GUIDE.md  | sed 's/{@link \([^#]*\)#\([^ ]*\)}/[\1#\2](https:\/\/mercmobily.github.io\/best-webdriver\/\1#\2/g'  | less

However:

  • The #method is not optional: it must be there. How do I make it non-mandatory?

  • The description is not considered at all. This is tricky, and I wonder if one SED regexp can do this. Basically, the input can be {@link Class#method blah blah} (the link text should be blah blah or {@link Class#method} (the link text should be Class#method)

If making one regexp that includes the optional regexp is too difficult, I will have to pass the file through sed twice.

Ideas?

Merc
  • 16,277
  • 18
  • 79
  • 122
  • No, you don't have to pass the file through `sed` twice. `sed` is a scripting language; you can specify as many commands as you like in a single `sed` script. See e.g. https://stackoverflow.com/questions/7657647/combining-2-sed-commands – tripleee Feb 01 '18 at 07:00
  • You'll probably also want to avoid the [useless use of `cat`](https://stackoverflow.com/questions/11710552/useless-use-of-cat) – tripleee Feb 01 '18 at 07:00
  • 1
    Wouldn't simply taking the first token after @link into the link work? `'s/{@link \([^ }]*\) \([^}]*\)}/[\1](\2)/g'` – tripleee Feb 01 '18 at 07:04

1 Answers1

2

It can be done with the two following sed substitutions (can be separated with newline or ; or multiple -e expressions), don't know how to do it with one in sed:

sed  '
s/{@link \([^} #]*\)\(#[^} #]*\)\? \([^} ]*\)}/[\3](https:\/\/mercmobily.github.io\/best-webdriver\/\1.html\2)/g
s/{@link \([^} #]*\)\(#[^} #]*\)\?}/[\1\2](https:\/\/mercmobily.github.io\/best-webdriver\/\1.html\2)/g
' << 'END'
{@link Class#method Description}
{@link Class#method}
{@link Class Description}
{@link Class}
END

Note to avoid to escape / another delimiter can be used , for example:

sed  '
s,{@link \([^} #]*\)\(#[^} #]*\)\? \([^} ]*\)},[\3](https://mercmobily.github.io/best-webdriver/\1.html\2),g
s,{@link \([^} #]*\)\(#[^} #]*\)\?},[\1\2](https://mercmobily.github.io/best-webdriver/\1.html\2),g
' << 'END'
{@link Class#method Description}
{@link Class#method}
{@link Class Description}
{@link Class}
END

Otherwise with perl can be done in one

perl -pe 's/\{\@link ([^} #]*)(#[^} #]*)?(?: ([^} ]*))?\}/"[".($3?$3:$1.$2)."](https:\/\/mercmobily.github.io\/best-webdriver\/$1.html$2)"/ge' << 'END'
{@link Class#method Description}
{@link Class#method}
{@link Class Description}
{@link Class}
END

Note that expression ($3?$3:$1.$2) can be shortened ($3||$1.$2)

Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36