Ask Your Question
0

Replacing text with URL link

asked 2018-07-26 21:16:35 +0200

Pedja gravatar image

I need to search text in LibreOffice Writer using known criteria, find text excerpts and replace them with active URL links that contain found text. If found excerpt already is a link it should be skipped.

Example:

If I find TA0GED in text I have to replace it with active link in a manner that TA0GED is still displayed but converted to active link that leads to http://mysite.org/TA0GED.

I made regular expression that finds text excerpts as I need them.

T[EP][:digit:]+[:alpha:]{1,4}

It successfully finds everything. My problem is I have no idea what to do next. I manged to replace it with another string that contains found string but I cannot find proper way to convert it to a clickable link.

Internet search showed nothing on this topic.

Is that possible?

edit retag flag offensive close merge delete

Comments

If your document is plain text with little formatting, you can convert it to HTML, where inserting hyperlinks by replacing text is easy - you just add the necessary tags. In Writer, you cannot just add the link tags - you would have to insert the xml code, but you can't do that in a replace operation, as far as I know.

floris v gravatar imagefloris v ( 2018-07-26 21:48:16 +0200 )edit

Besides writing a macro, you could also try using FODT, which is a plain XML, and do the replacements there using a plain text editor.

By the way, your regex T[EP][:digit:]+[:alpha:]{1,4} does not match your sample TA0GED ;-)

Mike Kaganski gravatar imageMike Kaganski ( 2018-07-26 23:07:02 +0200 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2018-07-28 07:08:43 +0200

pierre-yves samyn gravatar image

updated 2018-07-28 07:10:59 +0200

Hi

You can get the result by a simple search-replace by installing the AltSearch extension.

In the following example, replace all questions numbers with a link to this site:

image description

  • Search for: [0-9]{6}
  • Replace: \h{https://ask.libreoffice.org/en/question/&}
  • Tick Regular expressions

You can click the Replace list to get help.

You must of course adapt to your needs :)

HTH - Regards

edit flag offensive delete link more
0

answered 2018-07-27 20:45:52 +0200

Lupp gravatar image

updated 2018-07-27 20:51:23 +0200

Along Andrew's "useful Informations" with a bit from macro recording:

REM  *****  BASIC  *****

Sub makeLinksInSelection(Optional pUrlMain As String, Optional pRegEx As String)
If IsMissing(pUrlMain) Then pUrlMain = "https://yourdomain.com/heap/"  'As an example'
If IsMissing(pRegEx)   Then pRegEx   = "T[EP][:digit:]+[:alpha:]{1,4}" 'As an example'
theSel  = ThisComponent.CurrentSelection
theText = ThisComponent.Text
sD      = ThisComponent.CreateSearchDescriptor
sD.SearchString            = pRegEx
sD.SearchRegularExpression = True

    'Adapted from a recorded Sub'
    slotMachine = ThisComponent.CurrentController.Frame
    dispatcher  = createUnoService("com.sun.star.frame.DispatchHelper")
    dim args1(4) as new com.sun.star.beans.PropertyValue
    args1(2).Name = "Hyperlink.Target"
    args1(2).Value = ""
    args1(3).Name = "Hyperlink.Name"
    args1(3).Value = ""
    args1(4).Name = "Hyperlink.Type"
    args1(4).Value = 1
For j = 0 To theSel.Count - 1
 searchRg    = theSel(j)
 searchNext  = searchRg.Start
 searchEnd   = searchRg.End
 Do
  searchNext  = ThisComponent.FindNext(searchNext, sD)
  If IsNull(searchNext) Then Exit Do
  If ThisComponent.Text.CompareRegionEnds(searchNext, searchEnd)=-1 Then Exit Do
  ThisComponent.CurrentController.Select(searchNext)
      args1(0).Name = "Hyperlink.Text"
      args1(0).Value = searchNext.String
      args1(1).Name = "Hyperlink.URL"
      args1(1).Value = pUrlMain & searchNext.String
      dispatcher.executeDispatch(slotMachine, ".uno:SetHyperlink", "", 0, args1()) 
 Loop
Next j
ThisComponent.CurrentController.Select(theSel)
End Sub
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-07-26 21:16:35 +0200

Seen: 317 times

Last updated: Jul 28 '18