From ec574d5e2828059331a4177ddd2f5cec7235b8cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phaneDucasse?= Date: Sun, 26 May 2024 10:39:29 +0200 Subject: [PATCH 1/2] nicer splitOnFirst users --- src/Microdown/MicArgumentList.class.st | 5 ++-- src/Microdown/MicInlineBlockWithUrl.class.st | 14 ++++----- .../SequenceableCollection.extension.st | 29 +++++++++++++++---- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/Microdown/MicArgumentList.class.st b/src/Microdown/MicArgumentList.class.st index 7f64ba9d..7ce8c755 100644 --- a/src/Microdown/MicArgumentList.class.st +++ b/src/Microdown/MicArgumentList.class.st @@ -179,8 +179,9 @@ MicArgumentList >> setNoDefaultButArguments: string [ "string is assumed to be on the form key=value&key=value" | pairs | pairs := string splitOn: $&. - pairs do: [ :p | |keyValue| - keyValue := p splitOnFirst: $=. + pairs do: [ :p | + | keyValue | + keyValue := p splitOnFirst: $= noneValue: nil. self at: keyValue first trim asSymbol put: keyValue second ] ] diff --git a/src/Microdown/MicInlineBlockWithUrl.class.st b/src/Microdown/MicInlineBlockWithUrl.class.st index bf577f6c..e3af821a 100644 --- a/src/Microdown/MicInlineBlockWithUrl.class.st +++ b/src/Microdown/MicInlineBlockWithUrl.class.st @@ -113,16 +113,14 @@ MicInlineBlockWithUrl >> children: anObject [ { #category : 'visiting' } MicInlineBlockWithUrl >> closeMe [ - "the link url - ![alt text](url) - url is allowed to have title in quotes - (url ""title"") " - | split title | - split := url splitOnFirst: Character space. + | split | + split := url splitOnFirst: Character space noneValue: nil. self reference: (MicResourceReference fromUri: split first). - self arguments ifNil: [ - title := (split second ifNil: [ '' ]) - trimBoth: [:char | {$". Character space} includes: char]. - self arguments: (MicArgumentList withString: title)] + self arguments + ifNil: [ | title | + title := split second ifNotNil: [ :s | s] ifNil: [ '' ]. + self arguments: (MicArgumentList withString: title) ] ] { #category : 'utilities' } diff --git a/src/Microdown/SequenceableCollection.extension.st b/src/Microdown/SequenceableCollection.extension.st index d867c93e..a8090913 100644 --- a/src/Microdown/SequenceableCollection.extension.st +++ b/src/Microdown/SequenceableCollection.extension.st @@ -1,10 +1,29 @@ Extension { #name : 'SequenceableCollection' } { #category : '*Microdown' } -SequenceableCollection >> splitOnFirst: char [ - | indexOfChar | - indexOfChar := self indexOf: char. - indexOfChar = 0 ifTrue: [ ^{self. nil} ]. - ^{self copyFrom: 1 to: indexOfChar -1. self copyFrom: indexOfChar +1 to: self size} +SequenceableCollection >> splitOnFirst: anObject [ + "Split the receiver on the first element that match anObject" + + "#(1 2 3 0 4 5 6 0 7) splitOnFirst: 0 >> #(#(1 2 3) #(4 5 6 0 7)) " + "#(1 2 3 0 4 5 6 0 7) splitOnFirst: 99 >> #(#(1 2 3 04 5 6 0 7) #() " + + | element | + element := self indexOf: anObject. + element = 0 ifTrue: [ ^ { self. #() } ]. + ^ { self copyFrom: 1 to: element -1 . self copyFrom: element + 1 to: self size } + +] + +{ #category : '*Microdown' } +SequenceableCollection >> splitOnFirst: anObject noneValue: aValue [ + "Split the receiver on the first element that match anObject" + + "#(1 2 3 0 4 5 6 0 7) splitOnFirst: 0 noneValue: 33>> #(#(1 2 3) #(4 5 6 0 7)) " + "#(1 2 3 0 4 5 6 0 7) splitOnFirst: 99 noneValue: #()>> #(#(1 2 3 04 5 6 0 7) #() " + + | element | + element := self indexOf: anObject. + element = 0 ifTrue: [ ^ { self. aValue } ]. + ^ { self copyFrom: 1 to: element -1 . self copyFrom: element + 1 to: self size } ] From dbdb9ba4f37b9cfba142a480dd823035f092717e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phaneDucasse?= Date: Sun, 26 May 2024 10:41:06 +0200 Subject: [PATCH 2/2] even nicer implementation --- src/Microdown/SequenceableCollection.extension.st | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Microdown/SequenceableCollection.extension.st b/src/Microdown/SequenceableCollection.extension.st index a8090913..737ad098 100644 --- a/src/Microdown/SequenceableCollection.extension.st +++ b/src/Microdown/SequenceableCollection.extension.st @@ -2,16 +2,12 @@ Extension { #name : 'SequenceableCollection' } { #category : '*Microdown' } SequenceableCollection >> splitOnFirst: anObject [ - "Split the receiver on the first element that match anObject" + "Split the receiver on the first element that match anObject, returns two sequenceable." "#(1 2 3 0 4 5 6 0 7) splitOnFirst: 0 >> #(#(1 2 3) #(4 5 6 0 7)) " "#(1 2 3 0 4 5 6 0 7) splitOnFirst: 99 >> #(#(1 2 3 04 5 6 0 7) #() " - | element | - element := self indexOf: anObject. - element = 0 ifTrue: [ ^ { self. #() } ]. - ^ { self copyFrom: 1 to: element -1 . self copyFrom: element + 1 to: self size } - + ^ self splitOnFirst: anObject noneValue: #() ] { #category : '*Microdown' }