<commit_before>Class {
	#name : #RTTextBuilder,
	#superclass : #RTBuilder,
	#instVars : [
		'wordHook'
	],
	#category : #'Roassal2-Builder-TextBuilder'
}

{ #category : #protocol }
RTTextBuilder >> addText: aText [
	| el |
	el := self buildText: aText.
	view add: el.
	^ el
]

{ #category : #protocol }
RTTextBuilder >> buildText: aText [
	| el |
	el := RTElement new.
	shapeBuilder elementOn: el.
	self buildText: aText in: el.
	^ el
]

{ #category : #protocol }
RTTextBuilder >> buildText: aString in: element [
	| lines |
	lines := RTGroup new.
	aString lineIndicesDo: [ :startLine :endLine :endLineWithEOL | 
		| line lineString |
		lineString := aString copyFrom: startLine to: endLine.
		line := RTGroup new.
		lineString substringsDo: [ :startWord :endWord :endWordWithSpaces | 
			| wordString wordElement |
			wordString := lineString copyFrom: startWord to: endWordWithSpaces.
			wordElement := RTElement on: wordString. 
			wordElement + RTLabel new. "TODO: use shape builder"
			wordHook value: wordElement value: ((startLine + startWord) to: (startLine + endWord)).
			interactionBuilder setUpElements: wordElement.
			line add: wordElement.
			view add: wordElement.
		].
		RTHorizontalLineLayout new gapSize: 0; on: line.
		lines add: line.
	].
	RTVerticalLineLayout new gapSize: 0; on: lines.
	RTNest new on: element nest: lines.
]

{ #category : #examples }
RTTextBuilder >> example01Highlighting [
	"
	self new exampleHighlighting
	"

	| builder |
	builder := RTTextBuilder new.
	builder interaction highlightColored: Color blue.
	builder addText: 'hello world'.
	builder open
]

{ #category : #examples }
RTTextBuilder >> example02ClickableWord [
	"
	self new example02ClickableWord
	"

	| builder |
	builder := RTTextBuilder new.
	builder interaction  
					highlightColored: Color blue;
					when: TRMouseClick do: [ :event | event element model inspect ].
	builder addText: 'hello world', String cr, 'hola mundo'.
	builder open
]

{ #category : #examples }
RTTextBuilder >> example03ShowCode [
	"
	self new example03ShowCode
	"

	| builder |
	builder := RTTextBuilder new.
	builder interaction  
					highlight;
					when: TRMouseClick do: [ :event | event element model inspect ].
	builder addText: (RTTextBuilder>>#example03ShowCode) getSource.
	builder open
]

{ #category : #initialization }
RTTextBuilder >> initialize [

	super initialize.
	wordHook := [ :anElement :anInterval | nil ].
]

{ #category : #hooks }
RTTextBuilder >> renderIn: aView [
	
]

{ #category : #accessing }
RTTextBuilder >> wordHook: aTwoArgBlock [

	wordHook := aTwoArgBlock
]
<commit_msg>RTTextBuilder: replaces tab character with spaces<commit_after>Class {
	#name : #RTTextBuilder,
	#superclass : #RTBuilder,
	#instVars : [
		'wordHook'
	],
	#category : #'Roassal2-Builder-TextBuilder'
}

{ #category : #protocol }
RTTextBuilder >> addText: aText [
	| el |
	el := self buildText: aText.
	view add: el.
	^ el
]

{ #category : #protocol }
RTTextBuilder >> buildText: aText [
	| el |
	el := RTElement new.
	shapeBuilder elementOn: el.
	self buildText: aText in: el.
	^ el
]

{ #category : #protocol }
RTTextBuilder >> buildText: aString in: element [
	| lines normalizedString |
	normalizedString := self normalizeString: aString.
	lines := RTGroup new.
	normalizedString lineIndicesDo: [ :startLine :endLine :endLineWithEOL | 
		| line lineString |
		lineString := normalizedString copyFrom: startLine to: endLine.
		line := RTGroup new.
		lineString substringsDo: [ :startWord :endWord :endWordWithSpaces | 
			| wordString wordElement |
			wordString := lineString copyFrom: startWord to: endWordWithSpaces.
			wordElement := RTElement on: wordString. 
			wordElement + RTLabel new. "TODO: use shape builder"
			wordHook value: wordElement value: ((startLine + startWord) to: (startLine + endWord)).
			interactionBuilder setUpElements: wordElement.
			line add: wordElement.
			view add: wordElement.
		].
		RTHorizontalLineLayout new gapSize: 0; on: line.
		lines add: line.
	].
	RTVerticalLineLayout new gapSize: 0; on: lines.
	RTNest new on: element nest: lines.
]

{ #category : #examples }
RTTextBuilder >> example01Highlighting [
	"
	self new exampleHighlighting
	"

	| builder |
	builder := RTTextBuilder new.
	builder interaction highlightColored: Color blue.
	builder addText: 'hello world'.
	builder open
]

{ #category : #examples }
RTTextBuilder >> example02ClickableWord [
	"
	self new example02ClickableWord
	"

	| builder |
	builder := RTTextBuilder new.
	builder interaction  
					highlightColored: Color blue;
					when: TRMouseClick do: [ :event | event element model inspect ].
	builder addText: 'hello world', String cr, 'hola mundo'.
	builder open
]

{ #category : #examples }
RTTextBuilder >> example03ShowCode [
	"
	self new example03ShowCode
	"

	| builder |
	builder := RTTextBuilder new.
	builder interaction  
					highlight;
					when: TRMouseClick do: [ :event | event element model inspect ].
	builder addText: (RTTextBuilder>>#example03ShowCode) getSource.
	builder open
]

{ #category : #initialization }
RTTextBuilder >> initialize [

	super initialize.
	wordHook := [ :anElement :anInterval | nil ].
]

{ #category : #protected }
RTTextBuilder >> normalizeString: aString [

	^ String streamContents: [ :aStream |
		aString do: [ :eachChar |
			eachChar = Character tab
				ifTrue: [ aStream nextPutAll: '    ' ]
				ifFalse: [ aStream nextPut: eachChar ]
			 ]		
	]
]

{ #category : #hooks }
RTTextBuilder >> renderIn: aView [
	
]

{ #category : #accessing }
RTTextBuilder >> wordHook: aTwoArgBlock [

	wordHook := aTwoArgBlock
]

{'fraction_non_alphanumeric': 0.10349578095402101, 'fraction_numerical': 0.0051661787497847425, 'mean_word_length': 3.3022222222222224, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5546029', 'n_tokens_mistral': 2047, 'n_tokens_neox': 1843, 'n_words': 505}