Skip to content

Commit

Permalink
Naïve autoformatting of Markdown headings
Browse files Browse the repository at this point in the history
  • Loading branch information
oin committed Sep 14, 2020
1 parent d953ad1 commit afdb5c9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 31 deletions.
35 changes: 35 additions & 0 deletions AttributedPlainText.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

NSString *NVHiddenDoneTagAttributeName = @"NVDoneTag";
NSString *NVHiddenBulletIndentAttributeName = @"NVBulletIndentTag";
NSString *NVHiddenHeadingTagAttributeName = @"NVHeadingTag";

static BOOL _StringWithRangeIsProbablyObjC(NSString *string, NSRange blockRange);

Expand Down Expand Up @@ -125,6 +126,7 @@ - (void)santizeForeignStylesForImporting {
[self restyleTextToFont:[[GlobalPrefs defaultPrefs] noteBodyFont] usingBaseFont:nil];
[self addLinkAttributesForRange:range];
[self addStrikethroughNearDoneTagsForRange:range];
[self boldenMarkdownHeadingLinesForRange:range];
}

- (BOOL)restyleTextToFont:(NSFont*)currentFont usingBaseFont:(NSFont*)baseFont {
Expand Down Expand Up @@ -317,6 +319,39 @@ static BOOL _StringWithRangeIsProbablyObjC(NSString *string, NSRange blockRange)
//this is left as an exercise to the anal-retentive reader
}

-(void)addAttributesForMarkdownHeadingLinesInRange:(NSRange)changedRange
{
if(![[GlobalPrefs defaultPrefs] autoFormatsMarkdownHeadings])
return;

NSCharacterSet *newlineSet = [NSCharacterSet newlineCharacterSet];
NSRange lineEndRange, scanRange = changedRange;
@try {
do {
lineEndRange = [[self string] rangeOfCharacterFromSet:newlineSet options:NSLiteralSearch range:scanRange];
if(lineEndRange.location == NSNotFound) {
lineEndRange = NSMakeRange(NSMaxRange(scanRange), 1);
}
NSRange thisLineRange = NSMakeRange(scanRange.location, lineEndRange.location - scanRange.location);
NSString *thisLine = [[self string] substringWithRange:thisLineRange];
if([thisLine hasPrefix:@"#"]) {
[self addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:NSUnderlineStyleSingle], NSUnderlineStyleAttributeName, [NSNull null], NVHiddenHeadingTagAttributeName, nil] range:NSMakeRange(thisLineRange.location, thisLineRange.length)];
} else if([self attribute:NVHiddenHeadingTagAttributeName existsInRange:thisLineRange]) {
[self removeAttribute:NVHiddenHeadingTagAttributeName range:thisLineRange];
[self removeAttribute:NSUnderlineStyleAttributeName range:thisLineRange];
}
scanRange = NSMakeRange(NSMaxRange(thisLineRange), changedRange.length - (NSMaxRange(thisLineRange) - changedRange.location));
if(scanRange.length > 0) {
scanRange = NSMakeRange(scanRange.location + 1, scanRange.length - 1);
} else {
break;
}
} while(NSMaxRange(scanRange) <= NSMaxRange(changedRange));
} @catch (NSException *e) {
NSLog(@"_%s(%@): %@", _cmd, NSStringFromRange(changedRange), e);
}
}

- (void)addStrikethroughNearDoneTagsForRange:(NSRange)changedRange {
//scan line by line
//if the line ends in " @done", then strikethrough everything prior and add NVHiddenDoneTagAttributeName
Expand Down
7 changes: 6 additions & 1 deletion GlobalPrefs.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
static NSString *TabKeyIndentsKey = @"TabKeyIndents";
static NSString *PastePreservesStyleKey = @"PastePreservesStyle";
static NSString *AutoFormatsDoneTagKey = @"AutoFormatsDoneTag";
static NSString *AutoFormatsMarkdownHeadingsKey = @"AutoFormatsMarkdownHeadings";
static NSString *AutoFormatsListBulletsKey = @"AutoFormatsListBullets";
static NSString *AutoSuggestLinksKey = @"AutoSuggestLinks";
static NSString *AutoIndentsNewLinesKey = @"AutoIndentsNewLines";
Expand Down Expand Up @@ -107,7 +108,8 @@ - (id)init {

[defaults registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], AutoSuggestLinksKey,
[NSNumber numberWithBool:YES], AutoFormatsDoneTagKey,
[NSNumber numberWithBool:YES], AutoFormatsDoneTagKey,
[NSNumber numberWithBool:YES], AutoFormatsMarkdownHeadingsKey,
[NSNumber numberWithBool:YES], AutoIndentsNewLinesKey,
[NSNumber numberWithBool:YES], AutoFormatsListBulletsKey,
[NSNumber numberWithBool:NO], UseSoftTabsKey,
Expand Down Expand Up @@ -352,6 +354,9 @@ - (void)setAutoFormatsDoneTag:(BOOL)value sender:(id)sender {
- (BOOL)autoFormatsDoneTag {
return [defaults boolForKey:AutoFormatsDoneTagKey];
}
- (BOOL)autoFormatsMarkdownHeadings {
return [defaults boolForKey:AutoFormatsMarkdownHeadingsKey];
}
- (BOOL)autoFormatsListBullets {
return [defaults boolForKey:AutoFormatsListBulletsKey];
}
Expand Down
60 changes: 30 additions & 30 deletions Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
<key>CFBundleExecutable</key>
<string>Notational Velocity</string>
<key>CFBundleGetInfoString</key>
<string>2.0 β5, Copyright © 2011 Zachary Schneirov, All Rights Reserved.</string>
<string>Based on 2.0 β5, Copyright © 2011 Zachary Schneirov, All Rights Reserved.</string>
<key>CFBundleIconFile</key>
<string>Notality</string>
<key>CFBundleIdentifier</key>
Expand All @@ -142,28 +142,43 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>2.0 β5</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleSignature</key>
<string>N†l√</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>net.notational.velocity</string>
</dict>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>nv</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>8</string>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>LSArchitecturePriority</key>
<array>
<string>x86_64</string>
<string>i386</string>
<string>ppc</string>
</array>
<key>LSMinimumSystemVersion</key>
<string>10.4</string>
<key>LSMinimumSystemVersionByArchitecture</key>
<dict>
<key>i386</key>
<string>10.4.4</string>
<key>x86_64</key>
<string>10.6.0</string>
<key>ppc</key>
<string>10.4</string>
<key>x86_64</key>
<string>10.6.0</string>
</dict>
<key>LSArchitecturePriority</key>
<array>
<string>x86_64</string>
<string>i386</string>
<string>ppc</string>
</array>
<key>NSAppleScriptEnabled</key>
<true/>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
Expand Down Expand Up @@ -197,31 +212,16 @@
</array>
</dict>
</array>
<key>OSAScriptingDefinition</key>
<string>Notation.sdef</string>
<key>SUCheckAtStartup</key>
<false/>
<key>SUScheduledCheckInterval</key>
<real>345600</real>
<key>SUFeedURL</key>
<string>http://notational.net/nvupdates.xml</string>
<key>SUPublicDSAKeyFile</key>
<string>dsa_pub.pem</string>
<key>NSAppleScriptEnabled</key>
<true/>
<key>OSAScriptingDefinition</key>
<string>Notation.sdef</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>net.notational.velocity</string>
</dict>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>nv</string>
</array>
</dict>
</array>
<key>SUScheduledCheckInterval</key>
<real>345600</real>
<key>SmartCrashReports_CompanyName</key>
<string>Notational.net</string>
<key>SmartCrashReports_EmailTicket</key>
Expand Down
1 change: 1 addition & 0 deletions LinkingEditor.m
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,7 @@ - (void)didChangeText {
[[self textStorage] addLinkAttributesForRange:changedRange];

[[self textStorage] addStrikethroughNearDoneTagsForRange:changedRange];
[[self textStorage] addAttributesForMarkdownHeadingLinesInRange:changedRange];

if (!isAutocompleting && !wasDeleting && [prefsController linksAutoSuggested] &&
![[self undoManager] isUndoing] && ![[self undoManager] isRedoing]) {
Expand Down
6 changes: 6 additions & 0 deletions Notation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,7 @@
CLANG_ENABLE_OBJC_WEAK = NO;
CODE_SIGN_IDENTITY = "-";
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 10;
DEAD_CODE_STRIPPING = NO;
ENABLE_STRICT_OBJC_MSGSEND = NO;
FRAMEWORK_SEARCH_PATHS = (
Expand Down Expand Up @@ -1856,6 +1857,7 @@
LD_GENERATE_MAP_FILE = YES;
LIBRARY_SEARCH_PATHS = "$(LIBRARY_SEARCH_PATHS)";
MACOSX_DEPLOYMENT_TARGET = 10.9;
MARKETING_VERSION = "2.0 β7";
OTHER_CFLAGS = "-I/usr/local/opt/openssl/include";
OTHER_LDFLAGS = (
"-whatsloaded",
Expand All @@ -1881,6 +1883,7 @@
CLANG_ENABLE_OBJC_WEAK = NO;
CODE_SIGN_IDENTITY = "-";
COPY_PHASE_STRIP = YES;
CURRENT_PROJECT_VERSION = 10;
DEAD_CODE_STRIPPING = YES;
DEPLOYMENT_POSTPROCESSING = YES;
ENABLE_STRICT_OBJC_MSGSEND = NO;
Expand Down Expand Up @@ -1923,6 +1926,7 @@
LIBRARY_SEARCH_PATHS = "$(LIBRARY_SEARCH_PATHS)";
MACOSX_DEPLOYMENT_TARGET = 10.9;
"MACOSX_DEPLOYMENT_TARGET[arch=x86_64]" = 10.9;
MARKETING_VERSION = "2.0 β7";
ONLY_ACTIVE_ARCH = NO;
OTHER_CFLAGS = "-I/usr/local/opt/openssl/include";
OTHER_LDFLAGS = "-L/usr/local/opt/openssl/lib";
Expand Down Expand Up @@ -1951,6 +1955,7 @@
CLANG_CXX_LIBRARY = "compiler-default";
CLANG_ENABLE_OBJC_WEAK = NO;
CODE_SIGN_IDENTITY = "-";
CURRENT_PROJECT_VERSION = 10;
ENABLE_STRICT_OBJC_MSGSEND = NO;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -1990,6 +1995,7 @@
INSTALL_PATH = "$(HOME)/Applications";
LIBRARY_SEARCH_PATHS = "$(LIBRARY_SEARCH_PATHS)";
MACOSX_DEPLOYMENT_TARGET = 10.9;
MARKETING_VERSION = "2.0 β7";
OTHER_CFLAGS = "-I/usr/local/opt/openssl/include";
OTHER_LDFLAGS = (
"-weak_framework",
Expand Down

0 comments on commit afdb5c9

Please sign in to comment.