@@ -25,49 +25,80 @@ extension InlineNode {
25
25
var size : MarkdownImageSize ? {
26
26
switch self {
27
27
case . text( let input) :
28
- let pattern = " \\ {(?:width \\ s*= \\ s*( \\ d+)px \\ s*)?(?:height \\ s*= \\ s*( \\ d+)px \\ s*)?(?:width \\ s*= \\ s*( \\ d+)px \\ s*)?(?:height \\ s*= \\ s*( \\ d+)px \\ s*)? \\ } "
28
+ // Trying first to found a fixed pattern match
29
+ let fixedPattern = " \\ {(?:width \\ s*= \\ s*( \\ d+)px \\ s*)?(?:height \\ s*= \\ s*( \\ d+)px \\ s*)?(?:width \\ s*= \\ s*( \\ d+)px \\ s*)?(?:height \\ s*= \\ s*( \\ d+)px \\ s*)? \\ } "
29
30
30
- guard let regex = try ? NSRegularExpression ( pattern : pattern , options : [ ] ) else {
31
- return nil
31
+ if let ( width , height ) = extract ( regexPattern : fixedPattern , from : input ) {
32
+ return MarkdownImageSize ( value : . fixed ( width , height ) )
32
33
}
33
34
34
- let range = NSRange ( input. startIndex..< input. endIndex, in: input)
35
- guard let match = regex. firstMatch ( in: input, options: [ ] , range: range) else {
36
- return nil
37
- }
38
-
39
- var width : CGFloat ?
40
- var height : CGFloat ?
35
+ // Trying then to found a relative pattern match
36
+ let relativePattern = " \\ {(?:width \\ s*= \\ s*( \\ d+)% \\ s*)?(?:height \\ s*= \\ s*( \\ d+)% \\ s*)?(?:width \\ s*= \\ s*( \\ d+)% \\ s*)?(?:height \\ s*= \\ s*( \\ d+)% \\ s*)? \\ } "
41
37
42
- if let widthRange = Range ( match. range ( at: 1 ) , in: input) , let widthValue = Int ( input [ widthRange] ) {
43
- width = CGFloat ( widthValue)
44
- } else if let widthRange = Range ( match. range ( at: 3 ) , in: input) , let widthValue = Int ( input [ widthRange] ) {
45
- width = CGFloat ( widthValue)
38
+ if let ( wRatio, hRatio) = extract ( regexPattern: relativePattern, from: input) {
39
+ return MarkdownImageSize ( value: . relative( ( wRatio ?? 100 ) / 100 , ( hRatio ?? 100 ) / 100 ) )
46
40
}
47
41
48
- if let heightRange = Range ( match. range ( at: 2 ) , in: input) , let heightValue = Int ( input [ heightRange] ) {
49
- height = CGFloat ( heightValue)
50
- } else if let heightRange = Range ( match. range ( at: 4 ) , in: input) , let heightValue = Int ( input [ heightRange] ) {
51
- height = CGFloat ( heightValue)
52
- }
53
-
54
- return MarkdownImageSize ( width: width, height: height)
42
+ return nil
55
43
default :
56
44
return nil
57
45
}
58
46
}
47
+
48
+ private func extract(
49
+ regexPattern pattern: String ,
50
+ from input: String
51
+ ) -> ( width: CGFloat ? , height: CGFloat ? ) ? {
52
+ guard let regex = try ? NSRegularExpression ( pattern: pattern, options: [ ] ) else {
53
+ return nil
54
+ }
55
+
56
+ let range = NSRange ( input. startIndex..< input. endIndex, in: input)
57
+ guard let match = regex. firstMatch ( in: input, options: [ ] , range: range) else {
58
+ return nil
59
+ }
60
+
61
+ var width : CGFloat ?
62
+ var height : CGFloat ?
63
+
64
+ if let widthRange = Range ( match. range ( at: 1 ) , in: input) , let widthValue = Int ( input [ widthRange] ) {
65
+ width = CGFloat ( widthValue)
66
+ } else if let widthRange = Range ( match. range ( at: 3 ) , in: input) , let widthValue = Int ( input [ widthRange] ) {
67
+ width = CGFloat ( widthValue)
68
+ }
69
+
70
+ if let heightRange = Range ( match. range ( at: 2 ) , in: input) , let heightValue = Int ( input [ heightRange] ) {
71
+ height = CGFloat ( heightValue)
72
+ } else if let heightRange = Range ( match. range ( at: 4 ) , in: input) , let heightValue = Int ( input [ heightRange] ) {
73
+ height = CGFloat ( heightValue)
74
+ }
75
+
76
+ return ( width, height)
77
+ }
59
78
}
60
79
61
80
/// A value type representating an image size suffix.
62
81
///
63
- /// Example: `{width=50px}`
82
+ /// Example:
83
+ /// - `{width=50px}`
84
+ /// - `{width=50%}`
64
85
///
65
- /// Suffix can be either
66
- /// - {width=50px}
67
- /// - {height=50px}
68
- /// - {width=50px height=100px}
69
- /// - {height=50px width=100px}
86
+ /// Suffix can either be:
87
+ /// - `{width=50px}`
88
+ /// - `{height=50px}`
89
+ /// - `{width=50px height=100px}`
90
+ /// - `{height=50px width=100px}`
91
+ /// - `{width=50%}`
92
+ /// - `{height=50%}`
93
+ /// - `{width=50% height=100%}`
94
+ /// - `{height=50% width=100%}`
70
95
struct MarkdownImageSize {
71
- let width : CGFloat ?
72
- let height : CGFloat ?
96
+ let value : Value
97
+
98
+ enum Value {
99
+ /// Represents a fixed value size:`.fixed(width, height)`
100
+ case fixed( CGFloat ? , CGFloat ? )
101
+ /// Represents a relative value size: `.relative(proportionalWidth, proportionalHeight)`
102
+ case relative( CGFloat , CGFloat )
103
+ }
73
104
}
0 commit comments