Marcel Taeumel uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-ct.850.mcz ==================== Summary ==================== Name: Collections-ct.850 Author: ct Time: 15 August 2019, 11:33:51.432735 pm UUID: f0af391e-59c7-4b41-815c-0aa3ae8afb6d Ancestors: Collections-fn.847 Implement formatting on Text Try out something like: 'Hello {1}!' asText format: {Utilities authorInitials asText allBold}. =============== Diff against Collections-fn.847 =============== Item was added: + ----- Method: Text class>>new:streamContents: (in category 'instance creation') ----- + new: newSize streamContents: blockWithArg + + | stream | + stream := TextStream on: (self new: newSize). + blockWithArg value: stream. + ^ stream contents! Item was added: + ----- Method: Text>>format: (in category 'formatting') ----- + format: arguments + "format the receiver with arguments, respecting the format both of receiver and collection elements" + + ^self class new: self size * 11 // 10 streamContents: [ :output | + | nextIndex | + nextIndex := 1. + [ nextIndex <= self size ] whileTrue: [ + (self at: nextIndex) caseOf: { + [$\] -> [ + nextIndex := nextIndex + 1. + output withAttributes: (runs at: nextIndex) do: [ + output nextPut: (self at: nextIndex) ] ]. + [${] -> [ + "Parse the index - a positive integer in base 10." + | character collectionIndex attributes | + collectionIndex := 0. + attributes := Set new. + [ (character := string at: (nextIndex := nextIndex + 1)) isDigit ] whileTrue: [ + collectionIndex := collectionIndex * 10 + character digitValue. + attributes addAll: (runs at: nextIndex) ]. + character = $} ifFalse: [ self error: '$} expected' ]. + output withAttributes: attributes do: [ + output nextPutAll: (arguments at: collectionIndex) asStringOrText ] ] } + otherwise: [ + output withAttributes: (runs at: nextIndex) do: [ + output nextPut: (self at: nextIndex) ] ]. + nextIndex := nextIndex + 1 ] ]! |
Thank you for this contribution. Both versions are now possible: ('<b>Hello {1} World!</b>' format: {'Smalltalk'}) asTextFromHtml. '<b>Hello {1} World!</b>' asTextFromHtml format: {'Smalltalk'} And of course text arguments. Note that there is no automatic conversion from a string pattern to a text pattern: 'Hello {1} World!' format: {'<b>Smalltalk</b>' asTextFromHtml}. This will return a string without the bold face. Which is fine, I guess. Best, Marcel
|
Free forum by Nabble | Edit this page |