Permalink
Browse files

Moved most of HTML dealing code to GBHTMLOutputGenerator.

This required some modifications to existing classes and some unit test updates.
  • Loading branch information...
1 parent c6d0d4f commit a35467a473f95ec9110d1d61b88628fcf211f573 @tomaz committed Nov 29, 2010
@@ -11,13 +11,11 @@
@interface GBApplicationSettingsProvider ()
-- (BOOL)isTopLevelStoreObject:(id)object;
- (NSString *)outputPathForObject:(id)object withExtension:(NSString *)extension;
- (NSString *)relativePathPrefixFromObject:(GBModelBase *)source toObject:(GBModelBase *)destination;
- (NSString *)htmlReferenceForObjectFromIndex:(GBModelBase *)object;
- (NSString *)htmlReferenceForTopLevelObject:(GBModelBase *)object fromTopLevelObject:(GBModelBase *)source;
- (NSString *)htmlReferenceForMember:(GBModelBase *)member prefixedWith:(NSString *)prefix;
-- (NSString *)htmlExtension;
@end
@@ -43,27 +41,11 @@ - (id)init {
return self;
}
-#pragma mark HTML paths and references handling
-
-- (NSString *)htmlOutputPath {
- return [self.outputPath stringByAppendingPathComponent:@"html"];
-}
-
-- (NSString *)htmlOutputPathForIndex {
- NSString *result = [self.htmlOutputPath stringByAppendingPathComponent:@"index"];
- return [result stringByAppendingPathExtension:[self htmlExtension]];
-}
-
-- (NSString *)htmlOutputPathForObject:(GBModelBase *)object {
- NSParameterAssert(object != nil);
- NSParameterAssert([self isTopLevelStoreObject:object]);
- NSString *inner = [self htmlReferenceForObjectFromIndex:object];
- return [self.htmlOutputPath stringByAppendingPathComponent:inner];
-}
+#pragma mark HTML references handling
- (NSString *)htmlReferenceNameForObject:(GBModelBase *)object {
NSParameterAssert(object != nil);
- if ([self isTopLevelStoreObject:object]) return [self htmlReferenceForObject:object fromSource:object];
+ if (object.isTopLevelObject) return [self htmlReferenceForObject:object fromSource:object];
return [self htmlReferenceForMember:object prefixedWith:@""];
}
@@ -73,7 +55,7 @@ - (NSString *)htmlReferenceForObject:(GBModelBase *)object fromSource:(GBModelBa
// Generate hrefs from index to objects:
if (!source) {
// To top-level object.
- if ([self isTopLevelStoreObject:object]) return [self htmlReferenceForObjectFromIndex:object];
+ if (object.isTopLevelObject) return [self htmlReferenceForObjectFromIndex:object];
// To a member of top-level object.
NSString *path = [self htmlReferenceForObjectFromIndex:object.parentObject];
@@ -82,11 +64,11 @@ - (NSString *)htmlReferenceForObject:(GBModelBase *)object fromSource:(GBModelBa
}
// Generate hrefs from member to other objects:
- if (![self isTopLevelStoreObject:source]) {
+ if (!source.isTopLevelObject) {
GBModelBase *sourceParent = source.parentObject;
// To the parent or another top-level object.
- if ([self isTopLevelStoreObject:object]) return [self htmlReferenceForObject:object fromSource:sourceParent];
+ if (object.isTopLevelObject) return [self htmlReferenceForObject:object fromSource:sourceParent];
// To same or another member of the same parent.
if (object.parentObject == sourceParent) return [self htmlReferenceForMember:object prefixedWith:@"#"];
@@ -98,7 +80,7 @@ - (NSString *)htmlReferenceForObject:(GBModelBase *)object fromSource:(GBModelBa
}
// From top-level object to samo or another top level object.
- if (object == source || [self isTopLevelStoreObject:object]) {
+ if (object == source || object.isTopLevelObject) {
return [self htmlReferenceForTopLevelObject:object fromTopLevelObject:source];
}
@@ -19,48 +19,24 @@
@protocol GBApplicationSettingsProviding
///---------------------------------------------------------------------------------------
-/// @name Template paths handling
+/// @name Paths handling
///---------------------------------------------------------------------------------------
/** The base path to template files used for generating various output files. */
@property (copy) NSString *templatesPath;
-///---------------------------------------------------------------------------------------
-/// @name Output paths handling
-///---------------------------------------------------------------------------------------
-
/** The base path of the generated files. */
@property (copy) NSString *outputPath;
-/** The base path of the HTML generated files.
-
- This value depends on `outputPath` and is automatically calculated.
-
- @see htmlOutputPathForObject:
- @see htmlOutputPathForIndex
- @see outputPath
- */
-@property (readonly) NSString *htmlOutputPath;
-
-/** Returns file name including full path for HTML file representing the given top-level object.
-
- This works for any top-level object: class, category or protocol. The path is automatically determined regarding to the object class.
+/** The list of all full or partial paths to be ignored.
- @param object The object for which to return the path.
- @return Returns the path.
- @exception NSException Thrown if the given object is `nil` or not top-level object.
- @see htmlOutputPathForIndex
- @see htmlOutputPath
+ It's recommended to check if a path string ends with any of the given paths before processing it. This should catch directory and file names properly as directories are processed first.
*/
-- (NSString *)htmlOutputPathForObject:(GBModelBase *)object;
+@property (retain) NSMutableArray *ignoredPaths;
-/** Returns file name including full path for HTML file representing the main index.
-
- @return Returns the path.
- @see htmlOutputPathForObject:
- @see htmlOutputPath
- */
-- (NSString *)htmlOutputPathForIndex;
+///---------------------------------------------------------------------------------------
+/// @name Application-wide HTML helpers
+///---------------------------------------------------------------------------------------
/** Returns HTML reference name for the given object.
@@ -95,15 +71,9 @@
*/
- (NSString *)htmlReferenceForObject:(GBModelBase *)object fromSource:(GBModelBase *)source;
-///---------------------------------------------------------------------------------------
-/// @name Other paths handling
-///---------------------------------------------------------------------------------------
-
-/** The list of all full or partial paths to be ignored.
-
- It's recommended to check if a path string ends with any of the given paths before processing it. This should catch directory and file names properly as directories are processed first.
+/** The file extension for html files.
*/
-@property (retain) NSMutableArray *ignoredPaths;
+@property (readonly) NSString *htmlExtension;
///---------------------------------------------------------------------------------------
/// @name Helper classes
View
@@ -8,23 +8,14 @@
#import "GBApplicationSettingsProviding.h"
#import "GBStoreProviding.h"
-#import "GBDataObjects.h"
-#import "GBTemplateVariablesProvider.h"
-#import "GBTemplateHandler.h"
-#import "GBOutputGenerator.h"
+#import "GBHTMLOutputGenerator.h"
#import "GBGenerator.h"
@interface GBGenerator ()
-- (void)processClasses;
-- (void)processCategories;
-- (void)processProtocols;
-- (void)processIndex;
-- (void)writeString:(NSString *)string toFile:(NSString *)path;
-- (GBTemplateHandler *)templateHandlerFromTemplateFile:(NSString *)filename;
-@property (readonly) GBTemplateHandler *objectTemplate;
-@property (readonly) GBTemplateHandler *indexTemplate;
-@property (readonly) GBTemplateVariablesProvider *variablesProvider;
+- (void)setupGeneratorStepsWithStore:(id<GBStoreProviding>)store;
+- (void)runGeneratorStepsWithStore:(id<GBStoreProviding>)store;
+@property (readonly) NSMutableArray *outputGenerators;
@property (retain) id<GBApplicationSettingsProviding> settings;
@property (retain) id<GBStoreProviding> store;
@@ -55,107 +46,42 @@ - (id)initWithSettingsProvider:(id)settingsProvider {
- (void)generateOutputFromStore:(id<GBStoreProviding>)store {
NSParameterAssert(store != nil);
- GBLogVerbose(@"Generating output from parsed objects...");
- self.store = store;
- GBOutputGenerator *g = [[GBOutputGenerator alloc] init];
- [g copyTemplateFilesFromPath:self.settings.templatesPath toPath:self.settings.htmlOutputPath];
-// [self processClasses];
-// [self processCategories];
-// [self processProtocols];
-// [self processIndex];
+ GBLogInfo(@"Generating output from parsed objects...");
+ [self setupGeneratorStepsWithStore:store];
+ [self runGeneratorStepsWithStore:store];
}
-- (void)processClasses {
- for (GBClassData *class in self.store.classes) {
- GBLogInfo(@"Generating output for class %@...", class);
- NSDictionary *vars = [self.variablesProvider variablesForClass:class withStore:self.store];
- NSString *output = [self.objectTemplate renderObject:vars];
- NSString *path = [self.settings htmlOutputPathForObject:class];
- [self writeString:output toFile:path];
- GBLogDebug(@"Finished generating output for class %@.", class);
- }
-}
-
-- (void)processCategories {
- for (GBCategoryData *category in self.store.categories) {
- GBLogInfo(@"Generating output for category %@...", category);
- NSDictionary *vars = [self.variablesProvider variablesForCategory:category withStore:self.store];
- NSString *output = [self.objectTemplate renderObject:vars];
- NSString *path = [self.settings htmlOutputPathForObject:category];
- [self writeString:output toFile:path];
- GBLogDebug(@"Finished generating output for category %@.", category);
- }
-}
-
-- (void)processProtocols {
- for (GBProtocolData *protocol in self.store.protocols) {
- GBLogInfo(@"Generating output for protocol %@...", protocol);
- NSDictionary *vars = [self.variablesProvider variablesForProtocol:protocol withStore:self.store];
- NSString *output = [self.objectTemplate renderObject:vars];
- NSString *path = [self.settings htmlOutputPathForObject:protocol];
- [self writeString:output toFile:path];
- GBLogDebug(@"Finished generating output for protocol %@.", protocol);
- }
+- (void)setupGeneratorStepsWithStore:(id<GBStoreProviding>)store {
+ GBLogDebug(@"Initializing generation steps...");
+ [self.outputGenerators addObject:[GBHTMLOutputGenerator generatorWithSettingsProvider:self.settings]];
}
-- (void)processIndex {
- GBLogInfo(@"Generating output for index…");
- if ([self.store.classes count] > 0 || [self.store.protocols count] > 0 || [self.store.categories count] > 0) {
- NSDictionary *vars = [self.variablesProvider variablesForIndexWithStore:self.store];
- NSString *output = [self.indexTemplate renderObject:vars];
- NSString *path = [self.settings htmlOutputPathForIndex];
- [self writeString:output toFile:path];
- }
- GBLogDebug(@"Finished generating output for index.");
+- (void)runGeneratorStepsWithStore:(id<GBStoreProviding>)store {
+ GBLogDebug(@"Running generation steps...");
+ NSUInteger stepsCount = [self.outputGenerators count];
+ [self.outputGenerators enumerateObjectsUsingBlock:^(GBOutputGenerator *generator, NSUInteger idx, BOOL *stop) {
+ NSError *error = nil;
+ GBLogVerbose(@"Step %ld/%ld: Running %@...", idx, stepsCount, [generator className]);
+ if (![generator copyTemplateFilesToOutputPath:&error]) {
+ GBLogNSError(error, @"Step %ld/%ld failed: %@ failed copying template files to output, aborting!", idx, stepsCount, [generator className]);
+ *stop = YES;
+ return;
+ }
+ if (![generator generateOutputWithStore:store error:&error]) {
+ GBLogNSError(error, @"Step %ld/%ld failed: %@ failed generaing output, aborting!", idx, stepsCount, [generator className]);
+ *stop = YES;
+ return;
+ }
+ }];
}
#pragma mark Template files handling
-- (GBTemplateVariablesProvider *)variablesProvider {
- static GBTemplateVariablesProvider *result = nil;
- if (!result) result = [[GBTemplateVariablesProvider alloc] initWithSettingsProvider:self.settings];
- return result;
-}
-
-- (GBTemplateHandler *)objectTemplate {
- static GBTemplateHandler *result = nil;
- if (!result) result = [self templateHandlerFromTemplateFile:@"object-template.html"];
- return result;
-}
-
-- (GBTemplateHandler *)indexTemplate {
- static GBTemplateHandler *result = nil;
- if (!result) result = [self templateHandlerFromTemplateFile:@"index-template.html"];
- return result;
-}
-
-#pragma mark Helper methods
-
-- (void)writeString:(NSString *)string toFile:(NSString *)path {
- // Writes the given string to the given path, creating all folders if they don't exist.
- NSError *error = nil;
-
- NSString *standardized = [path stringByStandardizingPath];
- NSString *directory = [standardized stringByDeletingLastPathComponent];
- [self.fileManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:&error];
- if (error) {
- GBLogNSError(error, @"Failed creating directory while writting %@!", path);
- return;
- }
-
- [string writeToFile:standardized atomically:YES encoding:NSUTF8StringEncoding error:&error];
- if (error) GBLogNSError(error, @"Failed writing %@!", path);
-}
-
-- (GBTemplateHandler *)templateHandlerFromTemplateFile:(NSString *)filename {
- NSError *error = nil;
- NSString *path = self.settings.templatesPath;
- GBLogDebug(@"Creating template handler for file %@ in template folder %@...", filename, path);
- path = [path stringByAppendingPathComponent:filename];
- GBTemplateHandler *result = [GBTemplateHandler handler];
- if (![result parseTemplateFromPath:[path stringByStandardizingPath] error:&error]) {
- GBLogNSError(error, @"Failed parsing template %@!", filename);
- return nil;
+- (NSMutableArray *)outputGenerators {
+ static NSMutableArray *result = nil;
+ if (!result) {
+ GBLogDebug(@"Initializing output generators array...");
+ result = [[NSMutableArray alloc] init];
}
return result;
}
@@ -0,0 +1,15 @@
+//
+// GBHTMLOutputGenerator.h
+// appledoc
+//
+// Created by Tomaz Kragelj on 29.11.10.
+// Copyright 2010 Gentle Bytes. All rights reserved.
+//
+
+#import "GBOutputGenerator.h"
+
+/** Concrete `GBOutputGenerator` that generates HTML files.
+ */
+@interface GBHTMLOutputGenerator : GBOutputGenerator
+
+@end
Oops, something went wrong.

0 comments on commit a35467a

Please sign in to comment.