You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.5KB

  1. //
  2. // MMElement.m
  3. // MMMarkdown
  4. //
  5. // Copyright (c) 2012 Matt Diephouse.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #import "MMElement.h"
  26. static NSString * __MMStringFromElementType(MMElementType type)
  27. {
  28. switch (type)
  29. {
  30. case MMElementTypeNone:
  31. return @"none";
  32. case MMElementTypeHeader:
  33. return @"header";
  34. case MMElementTypeParagraph:
  35. return @"paragraph";
  36. case MMElementTypeBlockquote:
  37. return @"blockquote";
  38. case MMElementTypeNumberedList:
  39. return @"ol";
  40. case MMElementTypeBulletedList:
  41. return @"ul";
  42. case MMElementTypeListItem:
  43. return @"li";
  44. case MMElementTypeCodeBlock:
  45. return @"code";
  46. case MMElementTypeHorizontalRule:
  47. return @"hr";
  48. case MMElementTypeHTML:
  49. return @"html";
  50. case MMElementTypeLineBreak:
  51. return @"br";
  52. case MMElementTypeStrikethrough:
  53. return @"del";
  54. case MMElementTypeStrong:
  55. return @"strong";
  56. case MMElementTypeEm:
  57. return @"em";
  58. case MMElementTypeCodeSpan:
  59. return @"code";
  60. case MMElementTypeImage:
  61. return @"image";
  62. case MMElementTypeLink:
  63. return @"link";
  64. case MMElementTypeMailTo:
  65. return @"mailto";
  66. case MMElementTypeEntity:
  67. return @"entity";
  68. case MMElementTypeDefinition:
  69. return @"definition";
  70. default:
  71. return @"unknown";
  72. }
  73. }
  74. @implementation MMElement
  75. {
  76. NSMutableArray *_innerRanges;
  77. NSMutableArray *_children;
  78. }
  79. #pragma mark - NSObject
  80. - (id)init
  81. {
  82. self = [super init];
  83. if (self)
  84. {
  85. _innerRanges = [NSMutableArray new];
  86. _children = [NSMutableArray new];
  87. }
  88. return self;
  89. }
  90. - (void)dealloc
  91. {
  92. [self.children makeObjectsPerformSelector:@selector(setParent:) withObject:nil];
  93. }
  94. - (NSString *)description
  95. {
  96. return [NSString stringWithFormat:@"<%@: %p; type=%@; range=%@>",
  97. NSStringFromClass(self.class), self, __MMStringFromElementType(self.type), NSStringFromRange(self.range)];
  98. }
  99. #pragma mark - Public Methods
  100. - (void)addInnerRange:(NSRange)aRange
  101. {
  102. [self willChangeValueForKey:@"innerRanges"];
  103. [_innerRanges addObject:[NSValue valueWithRange:aRange]];
  104. [self didChangeValueForKey:@"innerRanges"];
  105. }
  106. - (void)removeLastInnerRange
  107. {
  108. [self willChangeValueForKey:@"innerRanges"];
  109. [_innerRanges removeLastObject];
  110. [self didChangeValueForKey:@"innerRanges"];
  111. }
  112. - (void)addChild:(MMElement *)aChild
  113. {
  114. [self willChangeValueForKey:@"children"];
  115. [_children addObject:aChild];
  116. aChild.parent = self;
  117. [self didChangeValueForKey:@"children"];
  118. }
  119. - (void)removeChild:(MMElement *)aChild
  120. {
  121. [self willChangeValueForKey:@"children"];
  122. [_children removeObjectIdenticalTo:aChild];
  123. aChild.parent = nil;
  124. [self didChangeValueForKey:@"children"];
  125. }
  126. - (MMElement *)removeLastChild
  127. {
  128. MMElement *child = [self.children lastObject];
  129. [_children removeLastObject];
  130. return child;
  131. }
  132. #pragma mark - Public Properties
  133. - (void)setInnerRanges:(NSArray *)innerRanges
  134. {
  135. _innerRanges = [innerRanges mutableCopy];
  136. }
  137. - (void)setChildren:(NSArray *)children
  138. {
  139. for (MMElement *child in _children) {
  140. child.parent = nil;
  141. }
  142. _children = [children mutableCopy];
  143. for (MMElement *child in _children) {
  144. child.parent = self;
  145. }
  146. }
  147. @end