Initial import.

Only templates for now. This is based on some code from 2013 and adds a
proper build system and tests.
This commit is contained in:
Jonathan Schleifer 2016-05-06 20:04:28 +02:00
commit 96dec81b87
No known key found for this signature in database
GPG key ID: 338C3541DB54E169
21 changed files with 5444 additions and 0 deletions

21
src/Makefile Normal file
View file

@ -0,0 +1,21 @@
include ../extra.mk
SUBDIRS = exceptions
SHARED_LIB = ${OBJWEB_SHARED_LIB}
STATIC_LIB = ${OBJWEB_STATIC_LIB}
LIB_MAJOR = ${OBJWEB_LIB_MAJOR}
LIB_MINOR = ${OBJWEB_LIB_MINOR}
SRCS = OWTemplate.m
INCLUDES = ${SRCS:.m=.h} \
ObjWeb.h
OBJS_EXTRA = ${EXCEPTIONS_EXCEPTIONS_A}
LIB_OBJS_EXTRA = ${EXCEPTIONS_EXCEPTIONS_LIB_A}
include ../buildsys.mk
CPPFLAGS += -Iexceptions
LD = ${OBJC}

40
src/OWTemplate.h Normal file
View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2013, 2016, Jonathan Schleifer <js@webkeks.org>
*
* https://heap.zone/git/?p=objweb.git
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#import <ObjFW/ObjFW.h>
@interface OWTemplate: OFObject
{
OFString *_content;
}
@property (readonly, copy) OFString *content;
+ (instancetype)templateForName: (OFString*)name;
- (OFString*)contentWithVariables: (OFDictionary*)variables;
@end
@interface OFStream (OWTemplate)
- (void)writeTemplate: (OFString*)name;
- (void)writeTemplate: (OFString*)name
variables: (OFDictionary*)variables;
@end

155
src/OWTemplate.m Normal file
View file

@ -0,0 +1,155 @@
/*
* Copyright (c) 2013, 2016, Jonathan Schleifer <js@webkeks.org>
*
* https://heap.zone/git/?p=objweb.git
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#import "OWTemplate.h"
#import "OWTemplateMissingException.h"
static OFDictionary *templates = nil;
@implementation OWTemplate
@synthesize content = _content;
+ (void)initialize
{
void *pool;
OFFileManager *fileManager;
OFArray *files;
OFMutableDictionary *tmp;
if (self != [OWTemplate class])
return;
pool = objc_autoreleasePoolPush();
fileManager = [OFFileManager defaultManager];
files = [fileManager contentsOfDirectoryAtPath: @"templates"];
tmp = [OFMutableDictionary dictionaryWithCapacity: [files count]];
for (OFString *file in files) {
OFString *name, *path;
OWTemplate *template;
if (![file hasSuffix: @".html"])
continue;
name = [file stringByDeletingPathExtension];
path = [@"templates" stringByAppendingPathComponent: file];
template = [[[OWTemplate alloc] init] autorelease];
template->_content = [[OFString alloc]
initWithContentsOfFile: path];
[tmp setObject: template
forKey: name];
}
[tmp makeImmutable];
templates = [tmp retain];
objc_autoreleasePoolPop(pool);
}
+ (instancetype)templateForName: (OFString*)name
{
OWTemplate *template = [templates objectForKey: name];
if (template == nil)
@throw [OWTemplateMissingException exceptionWithName: name];
return template;
}
- (void)dealloc
{
[_content release];
[super dealloc];
}
- (OFString*)contentWithVariables: (OFDictionary*)variables
{
OFMutableString *ret = [OFMutableString string];
void *pool = objc_autoreleasePoolPush();
size_t i, last;
if ([variables count] == 0)
return [self content];
i = [_content rangeOfString: @"${"].location;
last = 0;
while (i < _content.length) {
OFString *name, *value;
size_t end;
[ret appendString:
[_content substringWithRange: of_range(last, i - last)]];
end = [_content
rangeOfString: @"}"
options: 0
range: of_range(i, _content.length - i)].location;
name = [_content
substringWithRange: of_range(i + 2, end - i - 2)];
value = [[variables objectForKey: name] description];
if (![name hasPrefix: @"="])
value = [value stringByXMLEscaping];
if (value == nil)
value = @"";
[ret appendString: value];
last = end + 1;
i = [_content
rangeOfString: @"${"
options: 0
range: of_range(end, _content.length - end)].location;
}
[ret appendString: [_content substringWithRange:
of_range(last, _content.length - last)]];
[ret makeImmutable];
objc_autoreleasePoolPop(pool);
return ret;
}
@end
@implementation OFStream (OWTemplate)
- (void)writeTemplate: (OFString*)name
{
[self writeString: [[OWTemplate templateForName: name] content]];
}
- (void)writeTemplate: (OFString*)name
variables: (OFDictionary*)variables
{
[self writeString: [[OWTemplate templateForName: name]
contentWithVariables: variables]];
}
@end

25
src/ObjWeb.h Normal file
View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 2013, 2016, Jonathan Schleifer <js@webkeks.org>
*
* https://heap.zone/git/?p=objweb.git
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#import "OWTemplate.h"
#import "OWTemplateMissingException.h"

12
src/exceptions/Makefile Normal file
View file

@ -0,0 +1,12 @@
include ../../extra.mk
STATIC_PIC_LIB_NOINST = ${EXCEPTIONS_LIB_A}
STATIC_LIB_NOINST = ${EXCEPTIONS_A}
SRCS = OWTemplateMissingException.m
INCLUDES = ${SRCS:.m=.h}
include ../../buildsys.mk
CPPFLAGS += -I..

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2013, 2016, Jonathan Schleifer <js@webkeks.org>
*
* https://heap.zone/git/?p=objweb.git
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#import <ObjFW/ObjFW.h>
@interface OWTemplateMissingException: OFException
{
OFString *_name;
}
@property (readonly, copy, nonatomic) OFString *name;
+ (instancetype)exceptionWithName: (OFString*)name;
- initWithName: (OFString*)name;
@end

View file

@ -0,0 +1,57 @@
/*
* Copyright (c) 2013, 2016, Jonathan Schleifer <js@webkeks.org>
*
* https://heap.zone/git/?p=objweb.git
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#import "OWTemplateMissingException.h"
@implementation OWTemplateMissingException
@synthesize name = _name;
+ (instancetype)exceptionWithName: (OFString*)name
{
return [[[self alloc] initWithName: name] autorelease];
}
- init
{
OF_INVALID_INIT_METHOD
}
- initWithName: (OFString*)name
{
self = [super init];
@try {
_name = [name copy];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (OFString*)description
{
return [OFString stringWithFormat:
@"The template %@ is missing!", _name];
}
@end