Ask Your Question
1

[Writer] Use another style for links [closed]

asked 2018-01-02 22:37:20 +0200

gnxr gravatar image

updated 2018-01-09 20:52:59 +0200

My document contains two kinds of hyperlinks:

  • Links to bookmarks (in the same document)
  • Links to web pages

I want these links to display differently. Since they are considered the same (why? we could have had a nice hierarchy of link styles), I want to define a new style, and I will have to apply it manually to every bookmark link. I called this style "Reference":

I have created a custom character style

This way I will have web links with the default blue, and bookmark links with the cute dotted underline. This means I don't want to edit the default style. I want to apply a different style to some of the links.

So I double-clicked on my "Reference" style and LO says the style has been applied. But however I look, nothing changed.

The custom style does not replace the default style.

How can I apply the custom style to my internal links?


EDIT The problem has been solved by resetting the default Internet link style to Standard (see @ajlittoz 's answer), and creating two new styles : DocLink (black + dotted underline) and WebLink (blue + single underline).

Following Jim K's advice, I also wrote a macro to automate the application of those styles. This is my first time writing a macro so it took me a little while to use correctly Basic and the LibreOffice API. Full source code (I don't know how to use syntax highlighting):

' Append an item to an Object array '
Private Sub append(array() as Object, item as Object)
    ReDim Preserve array(UBound(array) + 1)
    array(UBound(array)) = item
End Sub


' Find all links in the given document fragment '
' From: https://stackoverflow.com/questions/37611030 '
Private Sub findLinks(fragment as Object, links as Object)
    Dim portions as Object
    Dim portion as Object

    Select Case fragment.implementationName
        Case "SwXParagraph": ' Paragraphs are enumerable '
            Dim msg as String
            portions = fragment.createEnumeration

            While portions.hasMoreElements
                portion = portions.nextElement
                If Len(portion.HyperLinkURL) > 0 Then
                    ' Ignore internal LibreOffice links (#__) '
                    If Left(portion.HyperLinkURL, 3) <> "#__" Then
                        append(links, portion)
                    End If
                End If
            Wend

        Case "SwXTextTable": ' Tables have cells '
            Dim cells as Object
            Dim cell as Object
            Set cells = fragment.getCellNames()

            For i = LBound(cells) to UBound(cells)
                Set cell = fragment.getCellByName(cells(i))
                Set portions = cell.createEnumeration()
                While portions.hasMoreElements
                    findLinks(portions.nextElement, links)
                Wend
            Next
    End Select
End Sub


' Find all links (document & internet links) in the document '
' and apply a custom style depending on the link type '
Sub AutoLinkStyle
    ' I suppose in Writer "ThisComponent" is an instance of '
    ' "com.sun.star.text.TextDocument". Is ".Text" an alias '
    ' for ".getText()"? I couldnt find docs explaining it '
    Dim document as Object
    Dim fragments as Object
    Dim links() as Object
    Set document = ThisComponent
    Set fragments = document.Text.createEnumeration()

    ' Get a list of all links in the document '
    While fragments.hasMoreElements
        findLinks(fragments.nextElement, links)
    Wend

    ' Apply custom styles '
    Dim msg as String
    For i = LBound(links) to UBound(links)
        If Left(links(i).HyperLinkURL, 1) = "#" Then
            links(i).CharStyleName = "DocLink"
        Else
            links(i).CharStyleName = "WebLink"
        End If
    Next
End Sub
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by gnxr
close date 2018-01-07 16:31:38.100224

Comments

Did you forget to select the word(s) which are part of your link before applying your character style?

ajlittoz gravatar imageajlittoz ( 2018-01-02 23:25:34 +0200 )edit

@ajlittoz Nope, I even spent minutes re-applying said style over and over just to make sure. The "Styles and Formatting" panel highlights correctly my style when I select the link. When I put the cursor right after the link and write some more, the new text (not part of the link) has the right custom style. But it is never shown on the links.

gnxr gravatar imagegnxr ( 2018-01-03 00:05:47 +0200 )edit

This is a follow-up question from https://askubuntu.com/questions/99134....

Jim K gravatar imageJim K ( 2018-01-03 00:46:24 +0200 )edit

Good job writing the macro. For syntax highlighting on this site, single quotes ' must match, so add a ' to the end of each commented line as well as at the beginning.

Jim K gravatar imageJim K ( 2018-01-08 20:08:06 +0200 )edit

3 Answers

Sort by » oldest newest most voted
1

answered 2018-01-03 09:00:03 +0200

ajlittoz gravatar image

updated 2018-01-10 07:56:34 +0200

From my experiments, Internet Link does not behave as an ordinary character style: it seems to be an "internal" style which does not preclude others. As I understand it (I'm no developper and have not read yet any line of LO code), the style layers here are: character style, covered by Internet Link, partially covered by direct formatting (due to the "partial" effect, the correct ordering may be in fact: character style, direct formatting, Internet Link with standard overriding rules).

As an exception to standard behaviour, two character styles can be assigned to a link: a user one and Internet Link. The latter is a consequence (or the cause) of the link property and can only be removed by right-click and Remove Hyperlink.

If you need the hyperlink feature, you can't remove Internet Link but you can make profit of the double character style possibility to achieve different link appearance.

First, get Internet Link out of interference by giving it a "neutral" presentation:

  • Open Internet Link for modification
  • In tab Font Effects, push the Standard button; this will reset all attributes to the state of Default Style, i.e. no change to current paragraph style.
  • You can do the same if you want to override attributes in other tabs.
  • OK to save your new defintion.

Next, define as many character styles for your custom link appearance.

Apply the desired custom character style to the link. It retains the link property from Internet Link and displays graphical attributes from your character style.

I have not played with Visited Internet Link but the same trick should do.

I don't know if this is a hack and will persist across updates to code base.

@Jim K: you are not restricted to underline + blue color for links; it's just that Internet Link properties override those in the custom style until you reset attributes

If this answer helped you, please accept it by clicking the check mark ✔ to the left and, karma permitting, upvote it. If this resolves your problem, close the question, that will help other people with the same question.

edit flag offensive delete link more

Comments

Good point about the Standard button, which is much better than unzipping the file as suggested in my answer. Other than that, it sounds like we came to the same conclusion.

Jim K gravatar imageJim K ( 2018-01-03 18:40:25 +0200 )edit
1

Standard is not well described, if at all, and its importance/usefulness is highly underestimated. It saved my day more than once.

ajlittoz gravatar imageajlittoz ( 2018-01-04 08:43:59 +0200 )edit

Standard is exactly what I was looking for. Thanks!

gnxr gravatar imagegnxr ( 2018-01-07 16:34:26 +0200 )edit
1

answered 2018-01-03 03:26:38 +0200

Jim K gravatar image

updated 2018-01-03 03:30:33 +0200

Apparently, trying to use a different hyperlink style that does not have underlines or use blue color does not work. It will not override the "Internet Link" style for these attributes. DIrect formatting can override the style, but you are right to avoid direct formatting.

So let's do it the opposite way. Modify "Internet Link" and "Visited Internet Link" to look like "Default Style". This sounds easy, but it's actually the hard part. That's because removing the underline from within LibreOffice specifies "without underline" rather than leaving it unspecified. So instead, I unzipped the .odt file and edited styles.xml to remove these attributes from "Internet_20_link" and "Visited_20_Internet_20_link".

Now, create a new character style called "Underlined Link" to be used for internet hyperlinks. Set it to underlined and blue, and then find each internet link and set them to use this style. The result: non-underlined links to bookmarks.odt

only internet links are underlined

Regarding the other part of the question, with this approach, it will be necessary to format each internet hyperlink to use the "Underlined Link" style. If there are a lot of them, you could automate this with a macro that sets the styles based on the HyperLinkURL property.

edit flag offensive delete link more

Comments

I have lots of web links and bookmarks so I ended up writing a macro following your advice (see my updated question). Thanks!

gnxr gravatar imagegnxr ( 2018-01-07 16:39:04 +0200 )edit
0

answered 2018-01-02 23:45:46 +0200

RGB-es gravatar image

updated 2018-01-02 23:49:11 +0200

For Writer, bookmarks and web links are both "hyperlinks" and that's why they get the same character style. There is no way around to automate this, AFAIK.

Your only option is to not use bookmarks at all but cross references instead. Then to the cross reference marker you can apply any character style.

EDIT: as @ajlittoz says, you should be able to manually overwrite the character style, though.

edit flag offensive delete link more

Comments

Sadly I can't use cross references either since I don't want to use fields for these links. Cross references don't allow me to reference e.g. "My Word" with the text "my words". I replied to Ajlittoz's comment. It may be an issue with my machine (an mild-old laptop running Fedora), I will try tomorrow on a Windows computer.

gnxr gravatar imagegnxr ( 2018-01-03 00:14:14 +0200 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-02 22:37:20 +0200

Seen: 483 times

Last updated: Jan 10 '18