@@ -28,6 +28,7 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
2828 BOOL inUnorderedList = NO ;
2929 BOOL inOrderedList = NO ;
3030 BOOL inBlockQuote = NO ;
31+ BOOL inCodeBlock = NO ;
3132 unichar lastCharacter = 0 ;
3233
3334 for (int i = 0 ; i < text.length ; i++) {
@@ -94,7 +95,8 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
9495 [previousActiveStyles containsObject: @([H1Style getStyleType ])] ||
9596 [previousActiveStyles containsObject: @([H2Style getStyleType ])] ||
9697 [previousActiveStyles containsObject: @([H3Style getStyleType ])] ||
97- [previousActiveStyles containsObject: @([BlockQuoteStyle getStyleType ])]
98+ [previousActiveStyles containsObject: @([BlockQuoteStyle getStyleType ])] ||
99+ [previousActiveStyles containsObject: @([CodeBlockStyle getStyleType ])]
98100 ) {
99101 // do nothing, proper closing paragraph tags have been already appended
100102 } else {
@@ -127,6 +129,11 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
127129 inBlockQuote = NO ;
128130 [result appendString: @" \n </blockquote>" ];
129131 }
132+ // handle ending codeblock
133+ if (inCodeBlock && ![currentActiveStyles containsObject: @([CodeBlockStyle getStyleType ])]) {
134+ inCodeBlock = NO ;
135+ [result appendString: @" \n </codeblock>" ];
136+ }
130137
131138 // handle starting unordered list
132139 if (!inUnorderedList && [currentActiveStyles containsObject: @([UnorderedListStyle getStyleType ])]) {
@@ -143,14 +150,20 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
143150 inBlockQuote = YES ;
144151 [result appendString: @" \n <blockquote>" ];
145152 }
153+ // handle starting codeblock
154+ if (!inCodeBlock && [currentActiveStyles containsObject: @([CodeBlockStyle getStyleType ])]) {
155+ inCodeBlock = YES ;
156+ [result appendString: @" \n <codeblock>" ];
157+ }
146158
147159 // don't add the <p> tag if some paragraph styles are present
148160 if ([currentActiveStyles containsObject: @([UnorderedListStyle getStyleType ])] ||
149161 [currentActiveStyles containsObject: @([OrderedListStyle getStyleType ])] ||
150162 [currentActiveStyles containsObject: @([H1Style getStyleType ])] ||
151163 [currentActiveStyles containsObject: @([H2Style getStyleType ])] ||
152164 [currentActiveStyles containsObject: @([H3Style getStyleType ])] ||
153- [currentActiveStyles containsObject: @([BlockQuoteStyle getStyleType ])]
165+ [currentActiveStyles containsObject: @([BlockQuoteStyle getStyleType ])] ||
166+ [currentActiveStyles containsObject: @([CodeBlockStyle getStyleType ])]
154167 ) {
155168 [result appendString: @" \n " ];
156169 } else {
@@ -234,6 +247,8 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
234247 [result appendString: @" \n </ol>" ];
235248 } else if ([previousActiveStyles containsObject: @([BlockQuoteStyle getStyleType ])]) {
236249 [result appendString: @" \n </blockquote>" ];
250+ } else if ([previousActiveStyles containsObject: @([CodeBlockStyle getStyleType ])]) {
251+ [result appendString: @" \n </codeblock>" ];
237252 } else if (
238253 [previousActiveStyles containsObject: @([H1Style getStyleType ])] ||
239254 [previousActiveStyles containsObject: @([H2Style getStyleType ])] ||
@@ -257,6 +272,10 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
257272 inBlockQuote = NO ;
258273 [result appendString: @" \n </blockquote>" ];
259274 }
275+ if (inCodeBlock) {
276+ inCodeBlock = NO ;
277+ [result appendString: @" \n </codeblock>" ];
278+ }
260279 }
261280
262281 [result appendString: @" \n </html>" ];
@@ -327,8 +346,8 @@ - (NSString *)tagContentForStyle:(NSNumber *)style openingTag:(BOOL)openingTag l
327346 return @" h3" ;
328347 } else if ([style isEqualToNumber: @([UnorderedListStyle getStyleType ])] || [style isEqualToNumber: @([OrderedListStyle getStyleType ])]) {
329348 return @" li" ;
330- } else if ([style isEqualToNumber: @([BlockQuoteStyle getStyleType ])]) {
331- // blockquotes use <p> tags the same way lists use <li>
349+ } else if ([style isEqualToNumber: @([BlockQuoteStyle getStyleType ])] || [style isEqualToNumber: @([CodeBlockStyle getStyleType ])] ) {
350+ // blockquotes and codeblock use <p> tags the same way lists use <li>
332351 return @" p" ;
333352 }
334353 return @" " ;
@@ -446,6 +465,8 @@ - (NSString * _Nullable)initiallyProcessHtml:(NSString * _Nonnull)html {
446465 fixedHtml = [self stringByAddingNewlinesToTag: @" </ol>" inString: fixedHtml leading: YES trailing: YES ];
447466 fixedHtml = [self stringByAddingNewlinesToTag: @" <blockquote>" inString: fixedHtml leading: YES trailing: YES ];
448467 fixedHtml = [self stringByAddingNewlinesToTag: @" </blockquote>" inString: fixedHtml leading: YES trailing: YES ];
468+ fixedHtml = [self stringByAddingNewlinesToTag: @" <codeblock>" inString: fixedHtml leading: YES trailing: YES ];
469+ fixedHtml = [self stringByAddingNewlinesToTag: @" </codeblock>" inString: fixedHtml leading: YES trailing: YES ];
449470
450471 // line opening tags
451472 fixedHtml = [self stringByAddingNewlinesToTag: @" <p>" inString: fixedHtml leading: YES trailing: NO ];
@@ -518,14 +539,14 @@ - (NSArray *)getTextAndStylesFromHtml:(NSString *)fixedHtml {
518539 ongoingTags[currentTagName] = tagArr;
519540
520541 // skip one newline after opening tags that are in separate lines intentionally
521- if ([currentTagName isEqualToString: @" ul" ] || [currentTagName isEqualToString: @" ol" ] || [currentTagName isEqualToString: @" blockquote" ]) {
542+ if ([currentTagName isEqualToString: @" ul" ] || [currentTagName isEqualToString: @" ol" ] || [currentTagName isEqualToString: @" blockquote" ] || [currentTagName isEqualToString: @" codeblock " ] ) {
522543 i += 1 ;
523544 }
524545 } else {
525546 // we finish closing tags - pack tag name, tag range and optionally tag params into an entry that goes inside initiallyProcessedTags
526547
527548 // skip one newline that was added before some closing tags that are in separate lines
528- if ([currentTagName isEqualToString: @" ul" ] || [currentTagName isEqualToString: @" ol" ] || [currentTagName isEqualToString: @" blockquote" ]) {
549+ if ([currentTagName isEqualToString: @" ul" ] || [currentTagName isEqualToString: @" ol" ] || [currentTagName isEqualToString: @" blockquote" ] || [currentTagName isEqualToString: @" codeblock " ] ) {
529550 plainText = [[plainText substringWithRange: NSMakeRange (0 , plainText.length - 1 )] mutableCopy ];
530551 }
531552
@@ -643,6 +664,8 @@ - (NSArray *)getTextAndStylesFromHtml:(NSString *)fixedHtml {
643664 [styleArr addObject: @([OrderedListStyle getStyleType ])];
644665 } else if ([tagName isEqualToString: @" blockquote" ]) {
645666 [styleArr addObject: @([BlockQuoteStyle getStyleType ])];
667+ } else if ([tagName isEqualToString: @" codeblock" ]) {
668+ [styleArr addObject: @([CodeBlockStyle getStyleType ])];
646669 } else {
647670 // some other external tags like span just don't get put into the processed styles
648671 continue ;
0 commit comments