改行をBRタグに置き換える
Posted feedbacks - Smalltalk
Squeak Smalltalk で。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | | string accepts in out upToAnyOf letters separators replaceCRs |
string := '<a title="(>_<;)" href=''www.google.com''
name=''hoge'' target=_blank>link</a> <blink>a
nd</blink> <strong onClick=''alert("NG")''>click<br>me!</strong>'.
accepts := {#a->#(name href). #strong->#(). #br->#()} as: Dictionary.
string := string copyReplaceAll: '<br>' with: '<br/>'.
in := string readStream.
out := String new writeStream.
upToAnyOf := [:arr | String streamContents: [:ss |
arr := arr copyWith: nil.
[arr includes: in peek] whileFalse: [ss nextPut: in next]]].
letters := Character alphabet asArray, Character alphabet asUppercase.
separators := Character separators, #($/ $>).
replaceCRs := [:str | str copyReplaceAll: String cr with: '<br/>'].
[out nextPutAll: (replaceCRs value: (in upTo: $<) escapeEntities). in atEnd] whileFalse: [
| tag lt isClose isAccepted blank rest |
(isClose := in peek == $/) ifTrue: [in next].
tag := upToAnyOf value: separators.
lt := '<', (isClose ifTrue: ['/'] ifFalse: ['']).
(isAccepted := accepts keys includes: tag asLowercase) ifFalse: [lt := lt escapeEntities].
out nextPutAll: lt, tag.
[blank := upToAnyOf value: letters, '>'. {nil. $>} includes: in peek] whileFalse: [
| attr equal value quote |
attr := upToAnyOf value: #($= $>).
equal := in peek == $= ifTrue: [in next asString] ifFalse: [''].
value := (#($' $") includes: (quote := in peek))
ifTrue: [quote asString, (in next; upTo: quote), quote asString]
ifFalse: [upToAnyOf value: #($ $>)].
out nextPutAll: (isAccepted
ifFalse: [blank, attr, equal, value escapeEntities]
ifTrue: [((accepts at: tag) includes: attr)
ifTrue: [blank, attr, equal, value] ifFalse: ['']])].
rest := blank, (in peek == $> ifTrue: [in next asString] ifFalse: ['']).
out nextPutAll: (isAccepted ifTrue: [rest] ifFalse: [rest escapeEntities])].
World findATranscript: nil.
Transcript cr; show: out contents
"=> <a href='www.google.com'
name='hoge'>link</a> <blink>a<br/>nd</blink> <strong>click<br/>me!</strong> "
|


にしお
#3413()
Rating-2/2=-1.00
また、ユーザの入力注の<br>は<br/>に変換してください。
このお題はperezvonさんの提案を元にした三部作の二問目です。ご協力ありがとうございました。
[ reply ]