Search code examples
pythongtkpygtkpango

Convert pango markup string to GtkTextTag properties


I've got a gtk.TextView that I'd like to add markup-like text to. I know this can be achieved through the use of gtk.TextTag which you can create with similar properties as a pango markup string. I noticed there is no easy way to just say set_markup to a gtk.TextBuffer much like you can with multiple other widgets. Instead you have to create a TextTag, give it properties, and then insert it into the TextBuffer's TagTable specifying the iters that the tag applies to.

I'd ideally like to create a function that can convert a pango markup string into a TextTag to get the same effect. But gtk doesn't appear to have that functionality built-in. I've noticed that you can use pango.parse_markup() on a marked up string and it will create a pango.AttributeList which contains information regarding the properties set on the string and the indices that they occur at. But there are slight differences in each type of attribute that make it difficult to generalize for every case. Is there a better way to go about this? Or is pango markup just not meant to be converted into gtk.TextTag's?


Solution

  • I finally worked out my own solution to this problem. I created a function that parses the markup string (using pango.parse_markup). Through reading the documentation and python introspection, I was able to work out how to take pango.Attribute and turn convert it into properties that a GtkTextTag can use.

    Here's the function:

    def parse_markup_string(string):
        '''
        Parses the string and returns a MarkupProps instance
        '''
        #The 'value' of an attribute...for some reason the same attribute is called several different things...
        attr_values = ('value', 'ink_rect', 'logical_rect', 'desc', 'color')
    
        #Get the AttributeList and text
        attr_list, text, accel = pango.parse_markup( string )
        attr_iter = attr_list.get_iterator()
    
        #Create the converter
        props = MarkupProps()
        props.text = text
    
        val = True
        while val:
                attrs = attr_iter.get_attrs()
    
                for attr in attrs:
                        name = attr.type
                        start = attr.start_index
                        end = attr.end_index
                        name = pango.AttrType(name).value_nick
    
                        value = None
                        #Figure out which 'value' attribute to use...there's only one per pango.Attribute
                        for attr_value in attr_values:
                                if hasattr( attr, attr_value ):
                                        value = getattr( attr, attr_value )
                                        break
    
                        #There are some irregularities...'font_desc' of the pango.Attribute
                        #should be mapped to the 'font' property of a GtkTextTag
                        if name == 'font_desc':
                                name = 'font'
                        props.add( name, value, start, end )
    
                val = attr_iter.next()
    
        return props
    

    This function creates a MarkupProps() object that has the ability to generate GtkTextTags along with the index in the text to apply them to.

    Here's the object:

    class MarkupProps():
    '''
    Stores properties that contain indices and appropriate values for that property.
    Includes an iterator that generates GtkTextTags with the start and end indices to 
    apply them to
    '''
    def __init__(self): 
        '''
        properties = (  {   
                            'properties': {'foreground': 'green', 'background': 'red'}
                            'start': 0,
                            'end': 3
                        },
                        {
                            'properties': {'font': 'Lucida Sans 10'},
                            'start': 1,
                            'end':2,
    
                        },
                    )
        '''
        self.properties = []#Sequence containing all the properties, and values, organized by like start and end indices
        self.text = ""#The raw text without any markup
    
    def add( self, label, value, start, end ):
        '''
        Add a property to MarkupProps. If the start and end indices are already in
        a property dictionary, then add the property:value entry into
        that property, otherwise create a new one
        '''
        for prop in self.properties:
            if prop['start'] == start and prop['end'] == end:
                prop['properties'].update({label:value})
        else:
            new_prop =   {
                            'properties': {label:value},
                            'start': start,
                            'end':end,
                        }
            self.properties.append( new_prop )
    
    def __iter__(self):
        '''
        Creates a GtkTextTag for each dict of properties
        Yields (TextTag, start, end)
        '''
        for prop in self.properties:
            tag = gtk.TextTag()
            tag.set_properties( **prop['properties'] )
            yield (tag, prop['start'], prop['end'])
    

    So with this function and the MarkupProps object, I am able to, given a pango markup string, breakdown the string into it's properties, and text form, and then convert that into GtkTextTags.