Ask Your Question
1

Convert Anchor hyperlinks to references automatically?

asked 2017-11-25 17:47:10 +0100

leeand00 gravatar image

I have an HTML document with Anchor hyperlinks in it that jump to a section on the page and back to the original source again.

I take this HTML text and paste it into LibreOffice, and then when checking the navigator all the anchor-hyperlinks are listed, but they aren't references...

I can, of course, convert these to references, one at a time, so that when I export a pdf of the document I can jump between the same spots in the document, but this process is lengthy and takes considerable time to do, the more references that I have; seeing as I'm using a computer, I'd like to automate this process if possible; are there any star-basic apis that will allow me to do this?

edit retag flag offensive close merge delete

Comments

When I tried copying links and their anchor tags from HTML, the links still worked in Writer. Please attach an example document, or give steps to reproduce the problem including a webpage URL to copy from. For how to search and modify anchors of links, see https://stackoverflow.com/a/38537184/... and listing 5.54 of Andrew Pitonyak's macro document.

Jim K gravatar imageJim K ( 2017-11-26 11:43:12 +0100 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2017-11-26 20:50:47 +0100

librebel gravatar image

updated 2018-01-18 02:46:09 +0100

Hello @leeand00,

Assuming that the Hyperlinks’ internal targets are also defined inside your pasted document (under Bookmarks), it might be easier to change the Hyperlink’s target URL and set it to the jumpmark corresponding to the internal target, instead of converting them all into References.

For example if the base URL is “https://www.site.org/page” and the target URL is “https://www.site.org/page#section”, then you could just change the target URL into “#section” to make it jump internally.

I made a quick macro that does this automatically, but currently it only works in those cases where the given base URL string is exactly the same as the part before the “#” in the target URL.

Please try if the macro works already for your pages, else i’ll adjust the macro.

Sub Writer_Internalize_Hyperlinks( strBaseURL as String )
REM https://ask.libreoffice.org/en/question/139010/convert-anchor-hyperlinks-to-references-automatically/
REM After importing a webpage into Writer, it might contain Hyperlinks that are supposed to jump to another section on the page,
REM but instead they jump to that section on the internet web page itself.
REM This method sets the Target URL of such Hyperlinks to the corresponding Bookmark in Writer.
REM <strBaseURL> : full URL String of the page that contains the internal Hyperlinks, e.g. "https://www.site.org/page"
    Dim oDoc as Object  :   oDoc  = ThisComponent
    Dim oText as Object :   oText = oDoc.getText()
    Dim oParagraphs as Object     : oParagraphs = oText.createEnumeration()
    Dim oParagraph as Object
    Dim oTextPortions as Object
    Dim oTextPortion as Object
    Dim strURL as String

    Do While oParagraphs.hasMoreElements()
        oParagraph = oParagraphs.nextElement

        If oParagraph.supportsService( "com.sun.star.text.Paragraph" ) Then

            oTextPortions = oParagraph.createEnumeration()

            Do While oTextPortions.hasMoreElements()
                oTextPortion = oTextPortions.nextElement()

                strURL = oTextPortion.HyperlinkURL
                If strURL <> "" Then                REM it's a Hyperlink!'
                    If Left( strURL, Len( strBaseURL ) ) = strBaseURL Then  REM Only works if both URL formats are the same.
                        oTextPortion.HyperlinkURL = Mid( strURL, Len( strBaseURL ) + 1 )    REM keep jumpmark.
                    End If
                End If
            Loop
        End If
    Loop
End Sub
edit flag offensive delete link more
0

answered 2018-01-31 06:02:43 +0100

leeand00 gravatar image

Here's an updated version that gets the text inside the tables as well:

' Run THIS
Sub Writer_Internalize_Hyperlinks_new_test()
        ' Set THIS
    call Writer_Internalize_Hyperlinks_new("your-url-and-path-with-no-anchor")
End Sub

Sub Writer_Internalize_Hyperlinks_new(strBaseURL As String)


    REM https://ask.libreoffice.org/en/question/139010/convert-anchor-hyperlinks-to-references-automatically/
    REM After importing a webpage into Writer, it might contain Hyperlinks that are supposed to jump to another section on the page,
    REM but instead they jump to that section on the internet web page itself.
    REM This method sets the Target URL's of such Hyperlinks to the corresponding Bookmark in Writer.
    REM <strBaseURL> : full URL String of the page that contains the internal Hyperlinks, e.g. "https://www.site.org/page"


    ' Plain Text
    Dim oDoc as Object  :   oDoc  = ThisComponent
    Dim oText as Object :   oText = oDoc.getText()
    Dim oTXTParagraphs as Object     : oTXTParagraphs = oText.createEnumeration()

    Call Writer_RM_host_and_URI_but_leave_anchor(strBaseURL, oTXTParagraphs)


    ' Plain Text in Tables...

    Dim oSels As Object, oSel As Object
    Dim oTable As Variant, oCell As Object
    Dim tbls As Object

    tbls = ThisComponent.Texttables
    for i = 0 to tbls.Count-1:
      Rem MsgBox tbls.getByIndex(i).getName()

      oTextTable = tbls.getByIndex(i)

      ' MsgBox("tblName" & oTextTable.getName())

      set cellNames = oTextTable.getCellNames()

      for cellNum = 0 to Ubound(cellNames):                                       
        set cell = oTextTable.getCellByName(cellNames(cellNum))

        MsgBox("Cell name : " & cellNames(cellNum))


        Dim otblText as Object :   otblText = cell.getText()
        Dim otblParagraphs as Object     : otblParagraphs = otblText.createEnumeration()

        Call Writer_RM_host_and_URI_but_leave_anchor (strBaseURL, otblParagraphs)

      next cellNum
    next i  

End Sub

Sub Writer_RM_host_and_URI_but_leave_anchor(strBaseURL AS String, oParagraphs AS Object)

        Dim oParagraph as Object
        Dim oTextPortions as Object
        Dim oTextPortion as Object
        Dim strURL as String

        Do While oParagraphs.hasMoreElements()
            oParagraph = oParagraphs.nextElement

            If oParagraph.supportsService( "com.sun.star.text.Paragraph" ) Then

                oTextPortions = oParagraph.createEnumeration()

                Do While oTextPortions.hasMoreElements()
                    oTextPortion = oTextPortions.nextElement()

                    strURL = oTextPortion.HyperlinkURL
                    If strURL <> "" Then                REM it's a Hyperlink!
                        If Left( strURL, Len( strBaseURL ) ) = strBaseURL Then  REM Only works if both URL formats are the same.

                            oTextPortion.HyperlinkURL = mid(oTextPortion.HyperlinkURL, instr(oTextPortion.HyperlinkURL, "#"), Len(oTextPortion.HyperlinkURL))
                            ' MsgBox oTextPortion.HyperlinkURL

                        End If
                    End If
                Loop
            End If
        Loop
End Sub
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-11-25 17:47:10 +0100

Seen: 255 times

Last updated: Jan 31 '18