Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a3fe41fa15
Fetching contributors…

Cannot retrieve contributors at this time

file 175 lines (154 sloc) 7.444 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
//
// FountainWriter.m
//
// Copyright (c) 2012 Nima Yousefi & John August
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//

#import "FountainWriter.h"
#import "FNScript.h"
#import "FNElement.h"
#import "FountainRegexes.h"
#import "RegexKitLite.h"

@implementation FountainWriter

+ (NSString *)documentFromScript:(FNScript *)script
{
    NSString *documentContent = [self bodyFromScript:script];
    NSString *titlePageContent = [self titlePageFromScript:script];
    
    NSMutableString *document = [NSMutableString string];
    
    if ([titlePageContent length] > 0) {
        [document appendFormat:@"%@\n", titlePageContent];
    }
    
    if ([documentContent length] > 0) {
        [document appendFormat:@"%@", documentContent];
    }
    
    // trim newlines from the top and bottom of the document
    return [document stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

+ (NSString *)bodyFromScript:(FNScript *)script
{
    NSMutableString *fountainContent = [NSMutableString string];
    
    NSInteger dualDialogueCount = 0;
    
    for (FNElement *element in script.elements) {
        // data check
        if (([element.elementText isMatchedByRegex:@"^[\\s\\n\\t]*$"] || element.elementText == nil) && ![element.elementType isEqualToString:@"Page Break"]) {
            continue;
        }
        
        NSString *textToWrite = nil; // need to set to nil to avoid logic warnings
        if ([element.elementType isEqualToString:@"Comment"]) {
            // check if the comment text has any newlines, indicating that it's a multi-line comment
            textToWrite = [NSString stringWithFormat:@"\n[[%@]]", element.elementText];
        }
        else if ([element.elementType isEqualToString:@"Boneyard"]) {
            textToWrite = [NSString stringWithFormat:@"/*%@*/", element.elementText];
        }
        else if ([element.elementType isEqualToString:@"Synopsis"]) {
            textToWrite = [NSString stringWithFormat:@"=%@", element.elementText];
        }
        else if ([element.elementType isEqualToString:@"Scene Heading"]) {
            textToWrite = element.elementText;

            /*
We need to determine if the scene heading was a forced scene
heading, and if it was, we'll add a dot to the front of the text.
This is most easily done by checking to see if the text still
matches the SCENE_HEADER_PATTERN. Because we removed the preceeding
dot when in the parser, forced scene headings won't match the
original regular expression, while non-forced (unaltered) scene
headings will.
*/
            if (![[NSString stringWithFormat:@"\n%@\n", element.elementText] isMatchedByRegex:SCENE_HEADER_PATTERN]) {
                textToWrite = [NSString stringWithFormat:@".%@", textToWrite];
            }
            
            // Append a scene number if needed
            if (!script.suppressSceneNumbers && element.sceneNumber) {
                textToWrite = [NSString stringWithFormat:@"%@ #%@#", textToWrite, element.sceneNumber];
            }
        }
        else if ([element.elementType isEqualToString:@"Page Break"]) {
            textToWrite = @"====";
        }
        else if ([element.elementType isEqualToString:@"Section Heading"]) {
            NSMutableString *sectionDepthMarkup = [NSMutableString string];
            for (NSInteger depthLevel = 1; depthLevel <= element.sectionDepth; depthLevel++) {
                [sectionDepthMarkup appendString:@"#"];
            }
            textToWrite = [sectionDepthMarkup stringByAppendingString:element.elementText];
        }
        else if ([element.elementType isEqualToString:@"Transition"]) {
            if (![element.elementText isMatchedByRegex:TRANSITION_PATTERN]) {
                textToWrite = [NSString stringWithFormat:@"> %@", element.elementText];
            }
        }
        else {
            textToWrite = element.elementText;
        }
        
        if (element.isCentered) {
            // There should be a space between the end of the line and the < char, if there isn't already one there.
            if ([textToWrite isMatchedByRegex:@"[ ]$"]) {
                textToWrite = [NSString stringWithFormat:@"> %@<", textToWrite];
            }
            else {
                textToWrite = [NSString stringWithFormat:@"> %@ <", textToWrite];
            }
        }
        
        if ([element.elementType isEqualToString:@"Character"] && element.isDualDialogue) {
            dualDialogueCount++;
            if (dualDialogueCount == 2) {
                textToWrite = [NSString stringWithFormat:@"%@ ^", textToWrite];
                dualDialogueCount = 0;
            }
        }
        
        NSSet *dialogueTypes = [NSSet setWithObjects:@"Dialogue", @"Parenthetical", @"Comment", nil];
        if ([dialogueTypes containsObject:element.elementType]) {
            [fountainContent appendFormat:@"%@\n", textToWrite];
        }
        else {
            [fountainContent appendFormat:@"\n%@\n", textToWrite];
        }
    }
    return fountainContent;
}

+ (NSString *)titlePageFromScript:(FNScript *)script
{
    NSMutableString *titlePageContent = [NSMutableString string];

    if (!script.titlePage) {
        return titlePageContent;
    }
    
    for (NSDictionary *dict in script.titlePage) {
        [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            // Make the key pretty by capitalizing the first char
            NSString *keyString = [key stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[key substringToIndex:1] uppercaseString]];
            if ([obj count] == 1) {
                // Fix for authors vs author when only one author's name is given
                if ([key isEqualToString:@"authors"]) {
                    keyString = @"Author";
                }
                [titlePageContent appendFormat:@"%@: %@\n", keyString, [obj objectAtIndex:0]];
            }
            else {
                [titlePageContent appendFormat:@"%@:\n", keyString];
                for (NSString *value in obj) {
                    [titlePageContent appendFormat:@"\t%@\n", value];
                }
            }
        }];
    }
    return titlePageContent;
}


@end
Something went wrong with that request. Please try again.