gui/PPTextHighlighter.st
changeset 334 1db7e42031c8
equal deleted inserted replaced
333:2ffae473b494 334:1db7e42031c8
       
     1 "{ Package: 'stx:goodies/petitparser/gui' }"
       
     2 
       
     3 Object subclass:#PPTextHighlighter
       
     4 	instanceVariableNames:'parser attributeMapper'
       
     5 	classVariableNames:''
       
     6 	poolDictionaries:''
       
     7 	category:'PetitGui-Core'
       
     8 !
       
     9 
       
    10 
       
    11 !PPTextHighlighter methodsFor:'accessing'!
       
    12 
       
    13 attributeMapper 
       
    14 	"returns a dictionary with keys corresponding to parser names and 
       
    15 	values corresponding to a collection of TextAttributes"
       
    16 	^ attributeMapper
       
    17 !
       
    18 
       
    19 attributeMapper: aDictionary 
       
    20 	attributeMapper := aDictionary
       
    21 !
       
    22 
       
    23 parser
       
    24 	^ parser
       
    25 !
       
    26 
       
    27 parser: aParser
       
    28 	parser := aParser
       
    29 ! !
       
    30 
       
    31 !PPTextHighlighter methodsFor:'initialization'!
       
    32 
       
    33 initialize
       
    34 	parser := #any asParser.
       
    35 	attributeMapper := Dictionary new
       
    36 ! !
       
    37 
       
    38 !PPTextHighlighter methodsFor:'public'!
       
    39 
       
    40 addAttribute: aTextAttribute for: anElementString 
       
    41 	| attributes |
       
    42 	attributes := self attributeMapper at: anElementString ifAbsentPut: [OrderedCollection new].
       
    43 	attributes add: aTextAttribute
       
    44 !
       
    45 
       
    46 bold: anElementString
       
    47 	self addAttribute: TextEmphasis bold for: anElementString
       
    48 !
       
    49 
       
    50 color: anElementString with: aColor
       
    51 	self addAttribute: (TextColor new color: aColor) for: anElementString
       
    52 !
       
    53 
       
    54 highlight: aString
       
    55 	| text highlighter |
       
    56 	text := aString asText.
       
    57 	highlighter := parser transform: [ :p |
       
    58 		attributeMapper at: p name
       
    59 			ifPresent: [ :attributes | 
       
    60 				p token ==> [ :token | 
       
    61 					attributes do: [:each |
       
    62 						text 
       
    63 							addAttribute: each 
       
    64 							from: token start 
       
    65 							to: token stop ] ] ]
       
    66 			ifAbsent: [ p ] ].
       
    67 	highlighter parse: text.
       
    68 	^ text
       
    69 ! !
       
    70 
       
    71 !PPTextHighlighter class methodsFor:'documentation'!
       
    72 
       
    73 version
       
    74     ^ '$Header: /cvs/stx/stx/goodies/petitparser/gui/PPTextHighlighter.st,v 1.1 2014-03-04 21:15:00 cg Exp $'
       
    75 !
       
    76 
       
    77 version_CVS
       
    78     ^ '$Header: /cvs/stx/stx/goodies/petitparser/gui/PPTextHighlighter.st,v 1.1 2014-03-04 21:15:00 cg Exp $'
       
    79 ! !
       
    80