Skip to content

Commit

Permalink
Merge pull request #48 from SWTI2014/css/inline-css-support
Browse files Browse the repository at this point in the history
Fix #29, #33: Inline CSS and basic font formatting support
  • Loading branch information
MrSerth committed May 22, 2014
2 parents 02a5024 + 1ec427d commit ffa917c
Show file tree
Hide file tree
Showing 136 changed files with 531 additions and 132 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
accessing
attributeMap

^ attributeMap ifNil:
[ Dictionary newFrom: {
'font' -> [ :prop :fontAttribs | self readFontAttribute: prop propertyString to: fontAttribs ].
'font-style' -> [ :prop :fontAttribs | self readFontStyleAttribute: prop propertyString to: fontAttribs ].
'font-weight' -> [ :prop :fontAttribs | self readFontWeightAttribute: prop propertyString to: fontAttribs ].
'font-size' -> [ :prop :fontAttribs | self readFontSizeAttribute: prop propertyString to: fontAttribs ]
}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parsing
isValidStyleValue: aString

^ {'normal'. 'italic'. 'oblique'. 'initial'. 'inherit'} includes: aString
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parsing
isValidWeightValue: aString

^ {'normal'. 'bold'. 'bolder'. 'lighter'. '100'. '200'. '300'. '400'. '500'. '600'. '700'. '800'. '900'. 'initial'. 'inherit'} includes: aString
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parsing
parseTextAttributesFrom: aCSSProperty into: aContext

(self attributeMap at: aCSSProperty propertyName ifAbsent: [nil])
ifNotNilDo: [ :process | process value: aCSSProperty value: aContext ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
parsing
readFontAttribute: aString to: aContext
| values shorthandContext |
shorthandContext := Dictionary newFrom: {
#italic -> false.
#bold -> false.
#size -> TextStyle default defaultFontIndex
}.

values := aString splitOn: ' '.
values size >= 2 ifTrue: [
self readFontSizeAttribute: (values nextToLast splitOn: '/') first to: shorthandContext].
values size >= 3 ifTrue: [
(self isValidStyleValue: values first)
ifTrue: [ self readFontStyleAttribute: values first to: shorthandContext.
values size >= 4
ifTrue: [self readFontWeightAttribute: values second to: shorthandContext ]]
ifFalse: [
(self isValidWeightValue: values first)
ifTrue: [self readFontWeightAttribute: values first to: shorthandContext ]]].

aContext addAll: shorthandContext
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
parsing
readFontSizeAttribute: aString to: aContext
"Lots of value types possible: http://www.w3schools.com/cssref/pr_font_font-size.asp"
| defaultSize sizeFactors |
defaultSize := TextStyle default defaultFontIndex.
((aString endsWith: '%') or: (aString endsWith: 'em')) ifTrue: [
| parentFontSize |
"This is actually not the parent font size but the font size of a rule that should be overridden"
parentFontSize := aContext at: #size ifAbsent: [ defaultSize ].
aString asNumber ifNotNil: [:value |
(aString endsWith: '%')
ifTrue: [ aContext at: #size put: ((parentFontSize * (value / 100)) max: 1) asInteger ]
ifFalse: [ aContext at: #size put: ((parentFontSize * value) max: 1) asInteger ]].
^ self ].

(aString endsWith: 'cm') ifTrue:
[ aString asNumber ifNotNil: [:value | aContext at: #size put: (value * 4) asInteger].
^ self].

sizeFactors := Dictionary newFrom: {
'initial' -> 1.
'xx-small' -> 0.5.
'x-small' -> 0.6.
'small' -> 0.8.
'medium' -> 1.
'large' -> 1.5.
'x-large' -> 2.
'xx-large' -> 3.
}.
aContext at: #size put: ((sizeFactors at: aString ifAbsent: [1]) * defaultSize) asInteger
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parsing
readFontStyleAttribute: aString to: aContext
| map |
map := Dictionary newFrom: {
'italic' -> true.
'normal' -> false
}.

(map at: aString ifAbsent: [nil]) ifNotNilDo: [:value | aContext at: #italic put: value]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parsing
readFontWeightAttribute: aString to: aContext
| map |
map := Dictionary newFrom: {
'bold' -> true.
'normal' -> false
}.

(map at: aString ifAbsent: [nil]) ifNotNilDo: [:value | aContext at: #bold put: value]
12 changes: 12 additions & 0 deletions packages/HTML.package/CSSFontFormatter.class/methodProperties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"class" : {
},
"instance" : {
"attributeMap" : "rs 5/21/2014 12:46:49.11",
"isValidStyleValue:" : "rs 5/21/2014 15:44:29.914",
"isValidWeightValue:" : "rs 5/21/2014 15:43:47.846",
"parseTextAttributesFrom:into:" : "rs 5/21/2014 12:21:23.822",
"readFontAttribute:to:" : "rs 5/21/2014 16:06:04.462",
"readFontSizeAttribute:to:" : "rs 5/21/2014 16:15:51.59",
"readFontStyleAttribute:to:" : "rs 5/21/2014 11:48:15.832",
"readFontWeightAttribute:to:" : "rs 5/21/2014 11:48:27.886" } }
14 changes: 14 additions & 0 deletions packages/HTML.package/CSSFontFormatter.class/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"category" : "HTML-Formatter",
"classinstvars" : [
],
"classvars" : [
],
"commentStamp" : "",
"instvars" : [
"attributeMap" ],
"name" : "CSSFontFormatter",
"pools" : [
],
"super" : "CSSFormatter",
"type" : "normal" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
fontFormatter: anObject

fontFormatter := anObject
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
fontFormatter

^ fontFormatter
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
running
setUp
self fontFormatter: CSSFontFormatter new
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
testing
test01FontItalicBoldShouldBeParsedCorrectly
| prop result |
result := Dictionary new.
prop := CSSProperty new
propertyName: 'font';
propertyString: 'italic bold medium Verdana';
yourself.
self fontFormatter parseTextAttributesFrom: prop into: result.
self assert: true equals: (result at: #italic).
self assert: true equals: (result at: #bold)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
testing
test02FontStyleItalicShouldBeParsedCorrectly
| prop result |
result := Dictionary new.
prop := CSSProperty new
propertyName: 'font-style';
propertyString: 'italic';
yourself.
self fontFormatter parseTextAttributesFrom: prop into: result.
self assert: (result at: #italic) equals: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
testing
test03FontWeightBoldShouldBeParsedCorrectly
| prop result |
result := Dictionary new.
prop := CSSProperty new
propertyName: 'font-weight';
propertyString: 'bold';
yourself.
self fontFormatter parseTextAttributesFrom: prop into: result.
self assert: (result at: #bold) equals: true.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
testing
test04ComplexFontValueShouldBeParsedCorrectly
| prop result |
result := Dictionary new.
prop := CSSProperty new
propertyName: 'font';
propertyString: 'bold xx-large/180% Verdana';
yourself.
self fontFormatter parseTextAttributesFrom: prop into: result.
self assert: false equals: (result at: #italic).
self assert: true equals: (result at: #bold).
self assert: 3*2 equals: (result at: #size)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"class" : {
},
"instance" : {
"fontFormatter" : "rs 5/19/2014 19:50:21.108",
"fontFormatter:" : "rs 5/19/2014 19:50:21.12",
"setUp" : "rs 5/19/2014 19:50:35.284",
"test01FontItalicBoldShouldBeParsedCorrectly" : "rs 5/21/2014 16:21:36.626",
"test02FontStyleItalicShouldBeParsedCorrectly" : "rs 5/21/2014 16:21:29.714",
"test03FontWeightBoldShouldBeParsedCorrectly" : "rs 5/21/2014 16:21:22.976",
"test04ComplexFontValueShouldBeParsedCorrectly" : "rs 5/21/2014 16:21:15.452" } }
14 changes: 14 additions & 0 deletions packages/HTML.package/CSSFontFormatterTest.class/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"category" : "HTML-Tests",
"classinstvars" : [
],
"classvars" : [
],
"commentStamp" : "",
"instvars" : [
"fontFormatter" ],
"name" : "CSSFontFormatterTest",
"pools" : [
],
"super" : "TestCase",
"type" : "normal" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
as yet unclassified
parseTextAttributesFrom: styles into: aContext
self subclassResponsibility
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"class" : {
},
"instance" : {
"parseTextAttributesFrom:into:" : "rs 5/21/2014 11:55:57.472" } }
14 changes: 14 additions & 0 deletions packages/HTML.package/CSSFormatter.class/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"category" : "HTML-Formatter",
"classinstvars" : [
],
"classvars" : [
],
"commentStamp" : "",
"instvars" : [
],
"name" : "CSSFormatter",
"pools" : [
],
"super" : "Object",
"type" : "normal" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
accessing
cssPrefixMap
^ cssPrefixMap ifNil: [
Dictionary newFrom: {
'font' -> CSSFontFormatter new
}]
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
formatting commands
endHeader: level
boldLevel := boldLevel - 1. "self decreaseBold"
self ensureNewlines: 2.
self endFont: nil.
self ensureNewlines: 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
formatting commands
endStyles
self endFont: nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
formatting commands
getFormatterFor: aCSSProperty
| prefix |
prefix := self getPrefixOf: aCSSProperty propertyName.
^ self cssPrefixMap at: prefix ifAbsent: [nil]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
formatting commands
getPrefixOf: aString
(aString beginsWith: 'font') ifTrue: [ ^ 'font' ].
^ nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
private-formatting
getTextAttributesFrom: aDictionary
| attribs |
attribs := OrderedCollection new.
(aDictionary at: #italic ifAbsent: [nil]) ifNotNil: [:italic | italic ifTrue: [ attribs add: TextEmphasis italic ]].
(aDictionary at: #bold ifAbsent: [nil]) ifNotNil: [:bold | bold ifTrue: [ attribs add: TextEmphasis bold ]].
(aDictionary at: #size ifAbsent: [nil]) ifNotNil: [:size | attribs add: (TextFontChange fontNumber: size)].
^ attribs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
formatting commands
headerFont: level
^{TextFontChange fontNumber: ((5 - level) max: 1)}
^TextFontChange fontNumber: ((5 - level) max: 1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
formatting commands
parseCSSProperty: aCSSProperty into: aContext

(self getFormatterFor: aCSSProperty)
ifNotNilDo: [:formatter | formatter parseTextAttributesFrom: aCSSProperty into: aContext ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
formatting commands
parseTextAttributes: styles
| styleContext |
styleContext := Dictionary new.
styles do: [:style |
style properties do: [:prop | self parseCSSProperty: prop into: styleContext ]].

^ self getTextAttributesFrom: styleContext
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ formatting commands
startFont: aTextAttribList
"aTextAttribList is a collection of TextAttributes"
fontSpecs ifNil: [fontSpecs := OrderedCollection new].
fontSpecs add: aTextAttribList.
fontSpecs add:
(aTextAttribList isArray
ifTrue: [ OrderedCollection newFrom: aTextAttribList ]
ifFalse: [ aTextAttribList ]).
self setAttributes
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
formatting commands
startHeader: level
| attribs |
self ensureNewlines: 3.
boldLevel := boldLevel + 1. "self increaseBold"
self startFont: (self headerFont: level).
attribs := fontSpecs last.
attribs add: (self headerFont: level);
add: TextEmphasis bold.
self setAttributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
formatting commands
startStyles: styles
| textAttributes |
textAttributes := self parseTextAttributes: styles.
self startFont: textAttributes.
self setAttributes
16 changes: 12 additions & 4 deletions packages/HTML.package/DHtmlFormatter.class/methodProperties.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
"class" : {
},
"instance" : {
"cssPrefixMap" : "rs 5/19/2014 18:30:36.49",
"decreaseFontBy:" : "bolot 5/18/2000 11:55",
"endFont:" : "bolot 5/18/2000 11:23",
"endHeader:" : "LaurentLaffont 2/26/2010 23:13",
"headerFont:" : "bolot 5/18/2000 12:00",
"endHeader:" : "rs 5/18/2014 14:16:30.63",
"endStyles" : "rs 5/18/2014 14:00:49.41",
"getFormatterFor:" : "rs 5/21/2014 12:18:05.082",
"getPrefixOf:" : "rs 5/19/2014 18:31:23.964",
"getTextAttributesFrom:" : "rs 5/21/2014 12:45:42.142",
"headerFont:" : "rs 5/18/2014 14:15:07.55",
"increaseFontBy:" : "bolot 5/18/2000 11:50",
"lastFontSize" : "LaurentLaffont 2/26/2010 23:13",
"parseCSSProperty:into:" : "rs 5/21/2014 12:18:30.34",
"parseTextAttributes:" : "rs 5/21/2014 12:22:27.848",
"resetFont" : "bolot 5/18/2000 11:57",
"setAttributes" : "LaurentLaffont 2/26/2010 23:13",
"startFont:" : "LaurentLaffont 2/26/2010 23:13",
"startHeader:" : "LaurentLaffont 2/26/2010 23:13" } }
"startFont:" : "rs 5/18/2014 13:53:54.124",
"startHeader:" : "rs 5/18/2014 14:14:54.344",
"startStyles:" : "rs 5/18/2014 14:01:58.816" } }
3 changes: 2 additions & 1 deletion packages/HTML.package/DHtmlFormatter.class/properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
],
"commentStamp" : "<historical>",
"instvars" : [
"fontSpecs" ],
"fontSpecs",
"cssPrefixMap" ],
"name" : "DHtmlFormatter",
"pools" : [
],
Expand Down
Empty file.
Loading

0 comments on commit ffa917c

Please sign in to comment.