Skip to content

Commit

Permalink
manually incorporate Daniel's changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcher committed Aug 3, 2011
1 parent 32e66ee commit 58bdceb
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 111 deletions.
51 changes: 33 additions & 18 deletions GLibFacade.h
Original file line number Original file line Diff line number Diff line change
@@ -1,30 +1,43 @@
// /*
// GLibFacade.h * GLibFacade.h
// MultiMarkdown * MultiMarkdown
// *
// Created by Daniel Jalkut on 7/26/11. * Created by Daniel Jalkut on 7/26/11.
// Copyright 2011 __MyCompanyName__. All rights reserved. * Copyright 2011 __MyCompanyName__. All rights reserved.
// */


#import <Foundation/Foundation.h> #ifndef __MARKDOWN_GLIB_FACADE__
#define __MARKDOWN_GLIB_FACADE__

/* peg_markdown uses the link symbol for its own purposes */
#define link MARKDOWN_LINK_IGNORED
#include <unistd.h>
#undef link

#include <stdbool.h>
#include <ctype.h>


typedef int gboolean; typedef int gboolean;
typedef char gchar; typedef char gchar;


// WE implement minimal mirror implementations of GLib's GString and GSList /* This style of bool is used in shared source code */
// sufficient to cover the functionality required by MultiMarkdown. #define FALSE false
// #define TRUE true
// NOTE: THese are 100% clean, from-scratch implementations using only the
// GLib function prototype as guide for behavior. /* WE implement minimal mirror implementations of GLib's GString and GSList
// * sufficient to cover the functionality required by MultiMarkdown.
*
* NOTE: THese are 100% clean, from-scratch implementations using only the
* GLib function prototype as guide for behavior.
*/


typedef struct typedef struct
{ {
// Current UTF8 byte stream this string represents /* Current UTF8 byte stream this string represents */
char* str; char* str;


// Where in the str buffer will we add new characters /* Where in the str buffer will we add new characters */
// or append new strings? /* or append new strings? */
int currentStringBufferSize; int currentStringBufferSize;
int currentStringLength; int currentStringLength;
} GString; } GString;
Expand All @@ -39,7 +52,7 @@ void g_string_prepend(GString* baseString, char* prependedString);


void g_string_append_printf(GString* baseString, char* format, ...); void g_string_append_printf(GString* baseString, char* format, ...);


// Just implement a very simple singly linked list. /* Just implement a very simple singly linked list. */


typedef struct _GSList typedef struct _GSList
{ {
Expand All @@ -50,3 +63,5 @@ typedef struct _GSList
void g_slist_free(GSList* ripList); void g_slist_free(GSList* ripList);
GSList* g_slist_prepend(GSList* targetElement, void* newElementData); GSList* g_slist_prepend(GSList* targetElement, void* newElementData);
GSList* g_slist_reverse(GSList* theList); GSList* g_slist_reverse(GSList* theList);

#endif
34 changes: 19 additions & 15 deletions GLibFacade.m
Original file line number Original file line Diff line number Diff line change
@@ -1,15 +1,19 @@
// /*
// GLibFacade.m * GLibFacade.m
// MultiMarkdown * MultiMarkdown
// *
// Created by Daniel Jalkut on 7/26/11. * Created by Daniel Jalkut on 7/26/11.
// Copyright 2011 __MyCompanyName__. All rights reserved. * Copyright 2011 __MyCompanyName__. All rights reserved.
// */


#import <Foundation/Foundation.h> #include "GLibFacade.h"
#import "GLibFacade.h"


// GString #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

/* GString */


#define kStringBufferStartingSize 1024 #define kStringBufferStartingSize 1024
#define kStringBufferGrowthMultiplier 2 #define kStringBufferGrowthMultiplier 2
Expand Down Expand Up @@ -78,7 +82,7 @@ void g_string_append(GString* baseString, char* appendedString)
size_t newStringLength = baseString->currentStringLength + appendedStringLength; size_t newStringLength = baseString->currentStringLength + appendedStringLength;
ensureStringBufferCanHold(baseString, newStringLength); ensureStringBufferCanHold(baseString, newStringLength);


// We already know where the current string ends, so pass that as the starting address for strncat /* We already know where the current string ends, so pass that as the starting address for strncat */
strncat(baseString->str + baseString->currentStringLength, appendedString, appendedStringLength); strncat(baseString->str + baseString->currentStringLength, appendedString, appendedStringLength);
baseString->currentStringLength = newStringLength; baseString->currentStringLength = newStringLength;
} }
Expand Down Expand Up @@ -123,7 +127,7 @@ void g_string_prepend(GString* baseString, char* prependedString)
} }
} }


// GSList /* GSList */


void g_slist_free(GSList* ripList) void g_slist_free(GSList* ripList)
{ {
Expand All @@ -132,19 +136,19 @@ void g_slist_free(GSList* ripList)
{ {
GSList* nextItem = thisListItem->next; GSList* nextItem = thisListItem->next;


// I guess we don't release the data? Non-retained memory management is hard... let's figure it out later. /* I guess we don't release the data? Non-retained memory management is hard... let's figure it out later. */
free(thisListItem); free(thisListItem);


thisListItem = nextItem; thisListItem = nextItem;
} }
} }


// Currently only used for markdown_output.c endnotes printing /* Currently only used for markdown_output.c endnotes printing */
GSList* g_slist_reverse(GSList* theList) GSList* g_slist_reverse(GSList* theList)
{ {
GSList* lastNodeSeen = NULL; GSList* lastNodeSeen = NULL;


// Iterate the list items, tacking them on to our new reversed List as we find them /* Iterate the list items, tacking them on to our new reversed List as we find them */
GSList* listWalker = theList; GSList* listWalker = theList;
while (listWalker != NULL) while (listWalker != NULL)
{ {
Expand Down
43 changes: 27 additions & 16 deletions Makefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,24 +4,35 @@ VERSION=3.1b1


PROGRAM=multimarkdown PROGRAM=multimarkdown


# If you used fink to install gib2-dev, it might have been compiled for UNAME=$(shell uname)
# the i386 architecture, causing an error. Can try adding -arch i686 to
# CFLAGS ifeq ($(UNAME), Darwin)

define FINALNOTES
CFLAGS ?= -Wall -O3 -ansi ***\n\

*** WARNING: Since you are on Darwin, you probably meant to use the Xcode version instead.\n\
OBJS=markdown_parser.o markdown_output.o markdown_lib.o *** It does lots of nice magic like producing a version of the binary that is capable\n\
*** of running on multiple versions of Mac OS X and with a variety of CPU architectures.\n\
***
endef
else
FINALNOTES=Build complete.
endif

CFLAGS ?= -Wall -O3 -ansi -include GLibFacade.h -I ./ -D MD_USE_GET_OPT=1

OBJS=markdown_parser.o markdown_output.o markdown_lib.o GLibFacade.o
PEGDIR=peg-0.1.4 PEGDIR=peg-0.1.4
LEG=$(PEGDIR)/leg LEG=$(PEGDIR)/leg


$(LEG): $(LEG):
CC=gcc make -C $(PEGDIR) CC=gcc make -C $(PEGDIR)


%.o : %.c markdown_peg.h %.o : %.c markdown_peg.h
$(CC) -c $(CFLAGS) -o $@ $< $(CC) -c $(CFLAGS) -o $@ $<


$(PROGRAM) : markdown.c $(OBJS) $(PROGRAM) : markdown.c $(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS) $< $(CC) $(CFLAGS) -o $@ $(OBJS) $<
@echo "$(FINALNOTES)"


markdown_parser.c : markdown_parser.leg $(LEG) markdown_peg.h parsing_functions.c utility_functions.c markdown_parser.c : markdown_parser.leg $(LEG) markdown_peg.h parsing_functions.c utility_functions.c
$(LEG) -o $@ $< $(LEG) -o $@ $<
Expand All @@ -41,25 +52,25 @@ clean:
distclean: clean distclean: clean
make -C $(PEGDIR) spotless make -C $(PEGDIR) spotless


test: # $(PROGRAM) test: $(PROGRAM)
cd MarkdownTest; \ cd MarkdownTest; \
./MarkdownTest.pl --Script=../$(PROGRAM) --Tidy --Flags="-c" ./MarkdownTest.pl --Script=../$(PROGRAM) --Tidy --Flags="--compatibility"


mmdtest: # $(PROGRAM) mmdtest: $(PROGRAM)
cd MarkdownTest; \ cd MarkdownTest; \
./MarkdownTest.pl --Script=../$(PROGRAM) --testdir=MultiMarkdownTests ./MarkdownTest.pl --Script=../$(PROGRAM) --testdir=MultiMarkdownTests


compattest: # $(PROGRAM) compattest: $(PROGRAM)
cd MarkdownTest; \ cd MarkdownTest; \
./MarkdownTest.pl --Script=../$(PROGRAM) --Tidy --testdir=CompatibilityTests --Flags="-c" ./MarkdownTest.pl --Script=../$(PROGRAM) --testdir=CompatibilityTests --Flags="--compatibility"


latextest: # $(PROGRAM) latextest: $(PROGRAM)
cd MarkdownTest; \ cd MarkdownTest; \
./MarkdownTest.pl --Script=../$(PROGRAM) --testdir=MultiMarkdownTests --Flags="-t latex" --ext=".tex"; \ ./MarkdownTest.pl --Script=../$(PROGRAM) --testdir=MultiMarkdownTests --Flags="-t latex" --ext=".tex"; \
./MarkdownTest.pl --Script=../$(PROGRAM) --testdir=BeamerTests --Flags="-t latex" --ext=".tex"; \ ./MarkdownTest.pl --Script=../$(PROGRAM) --testdir=BeamerTests --Flags="-t latex" --ext=".tex"; \
./MarkdownTest.pl --Script=../$(PROGRAM) --testdir=MemoirTests --Flags="-t latex" --ext=".tex" ./MarkdownTest.pl --Script=../$(PROGRAM) --testdir=MemoirTests --Flags="-t latex" --ext=".tex"


xslttest: # $(PROGRAM) xslttest: $(PROGRAM)
cd MarkdownTest; \ cd MarkdownTest; \
./MarkdownTest.pl --Script=/bin/cat --testdir=MultiMarkdownTests \ ./MarkdownTest.pl --Script=/bin/cat --testdir=MultiMarkdownTests \
--TrailFlags="| ../Support/bin/mmd2tex-xslt" --ext=".tex"; \ --TrailFlags="| ../Support/bin/mmd2tex-xslt" --ext=".tex"; \
Expand Down
3 changes: 2 additions & 1 deletion glib.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
* *
*/ */


// Just a dummy file to keep the glib-dependent sources compiling as we would hope /* Just a dummy file to keep the glib-dependent sources compiling as we would hope */
#include "GLibFacade.h"
65 changes: 4 additions & 61 deletions markdown.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <getopt.h>
#include <glib.h> #include <glib.h>
#include "markdown_peg.h" #include "markdown_peg.h"


Expand All @@ -36,11 +37,10 @@ static int extensions;
***********************************************************************/ ***********************************************************************/


#define VERSION "3.1b1-glibfree" #define VERSION "3.1b1-redsweater"
#define COPYRIGHT "portions Copyright (c) 2010-2011 Fletcher T. Penney.\n" \ #define COPYRIGHT "portions Copyright (c) 2010-2011 Fletcher T. Penney.\n" \
"portions Copyright (c) 2011 Daniel Jalkut, MIT licensed.\n" \ "portions Copyright (c) 2011 Daniel Jalkut, MIT licensed.\n" \
"original Copyright (c) 2008-2009 John MacFarlane. License GPLv2+ or MIT.\n" \ "original Copyright (c) 2008-2009 John MacFarlane. License GPLv2+ or MIT.\n" \
"GlibCocoaBridge code Copyright (c) 2011 Daniel Jalkut.\n" \
"This is free software: you are free to change and redistribute it.\n" \ "This is free software: you are free to change and redistribute it.\n" \
"There is NO WARRANTY, to the extent permitted by law." "There is NO WARRANTY, to the extent permitted by law."


Expand All @@ -53,31 +53,10 @@ void version(const char *progname)
COPYRIGHT); COPYRIGHT);
} }


// We like to use getopt on the standalone Mac version, because
// it rids us of dependency on GLib's GOptions parser. But you
// might want to use this on other platforms that support getopt, too.
#ifndef MD_USE_GET_OPT
#if STANDALONE_MAC_VERSION
#define MD_USE_GET_OPT 1
#else
#define MD_USE_GET_OPT 0
#endif
#endif

// These are a bit hacky - note that the supplied arguments are not used to the fullest in the getopt case. For
// example we aren't able to use the specified description or in the case of strings, any specified output
// variables for the resulting string, we have to just catch those at processing time and create the strings.
#if MD_USE_GET_OPT
#include <getopt.h>
#define MD_ARGUMENT_FLAG(name, flagChar, flagValue, outPointer, desc, argPlaceholder) { name, no_argument, outPointer, outPointer ? flagValue : flagChar } #define MD_ARGUMENT_FLAG(name, flagChar, flagValue, outPointer, desc, argPlaceholder) { name, no_argument, outPointer, outPointer ? flagValue : flagChar }
#define MD_ARGUMENT_STRING(name, flagChar, outPointer, desc, argPlaceholder) { name, required_argument, NULL, flagChar } #define MD_ARGUMENT_STRING(name, flagChar, outPointer, desc, argPlaceholder) { name, required_argument, NULL, flagChar }
#else
#define MD_ARGUMENT_FLAG(name, flagChar, outPointer, desc, argPlaceholder) { name, flagChar, 0, G_OPTION_ARG_NONE, outPointer, desc, argPlaceholder }
#define MD_ARGUMENT_STRING(name, flagChar, outPointer, desc, argPlaceholder) { name, flagChar, 0, G_OPTION_ARG_STRING, outPointer, desc, argPlaceholder }
#endif


// With getopt we don't get the same fancy automatic usage (I don't think?) so for /* With getopt we don't get the same fancy automatic usage (I don't think?) so for now we're faking it ... */
// now we're faking it ...
static void printUsage() { static void printUsage() {
printf("Usage:\ printf("Usage:\
multimarkdown [OPTION...] [FILE...]\n\ multimarkdown [OPTION...] [FILE...]\n\
Expand Down Expand Up @@ -140,14 +119,9 @@ int main(int argc, char * argv[]) {
static gchar *opt_extract_meta = FALSE; static gchar *opt_extract_meta = FALSE;
static gboolean opt_no_labels = FALSE; static gboolean opt_no_labels = FALSE;


#if MD_USE_GET_OPT
static struct option entries[] = static struct option entries[] =
{ {
MD_ARGUMENT_FLAG( "help", 'h', 1, NULL, "Show help options", NULL ), MD_ARGUMENT_FLAG( "help", 'h', 1, NULL, "Show help options", NULL ),
#else
static GOptionEntry entries[] =
{
#endif
MD_ARGUMENT_FLAG( "version", 'v', 1, &opt_version, "print version and exit", NULL ), MD_ARGUMENT_FLAG( "version", 'v', 1, &opt_version, "print version and exit", NULL ),
MD_ARGUMENT_STRING( "output", 'o', &opt_output, "send output to FILE (default is stdout)", "FILE" ), MD_ARGUMENT_STRING( "output", 'o', &opt_output, "send output to FILE (default is stdout)", "FILE" ),
MD_ARGUMENT_STRING( "to", 't', &opt_to, "convert to FORMAT (default is html)", "FORMAT" ), MD_ARGUMENT_STRING( "to", 't', &opt_to, "convert to FORMAT (default is html)", "FORMAT" ),
Expand All @@ -157,25 +131,14 @@ int main(int argc, char * argv[]) {
MD_ARGUMENT_FLAG( "compatibility", 'c', 1, &opt_compatibility, "markdown compatibility mode", NULL ), MD_ARGUMENT_FLAG( "compatibility", 'c', 1, &opt_compatibility, "markdown compatibility mode", NULL ),
MD_ARGUMENT_FLAG( "batch", 'b', 1, &opt_batchmode, "process multiple files automatically", NULL ), MD_ARGUMENT_FLAG( "batch", 'b', 1, &opt_batchmode, "process multiple files automatically", NULL ),
MD_ARGUMENT_STRING( "extract", 'e', &opt_extract_meta, "extract and display specified metadata", NULL ), MD_ARGUMENT_STRING( "extract", 'e', &opt_extract_meta, "extract and display specified metadata", NULL ),
// For GOptions, the arguments are split into two groups
#if !MD_USE_GET_OPT
{ NULL }
};

/* Options to active syntax extensions. These appear separately in --help. */
static GOptionEntry ext_entries[] =
{
#endif
MD_ARGUMENT_FLAG( "smart", 0, 1, &opt_smart, "use smart typography extension (on by default)", NULL ), MD_ARGUMENT_FLAG( "smart", 0, 1, &opt_smart, "use smart typography extension (on by default)", NULL ),
MD_ARGUMENT_FLAG( "nosmart", 0, 1, &opt_no_smart, "do not use smart typography extension", NULL ), MD_ARGUMENT_FLAG( "nosmart", 0, 1, &opt_no_smart, "do not use smart typography extension", NULL ),
MD_ARGUMENT_FLAG( "notes", 0, 1, &opt_notes, "use notes extension (on by default)", NULL ), MD_ARGUMENT_FLAG( "notes", 0, 1, &opt_notes, "use notes extension (on by default)", NULL ),
MD_ARGUMENT_FLAG( "nonotes", 0, 1, &opt_no_notes, "do not use notes extension", NULL ), MD_ARGUMENT_FLAG( "nonotes", 0, 1, &opt_no_notes, "do not use notes extension", NULL ),
MD_ARGUMENT_FLAG( "process-html", 0, 1, &opt_process_html, "process MultiMarkdown inside of raw HTML", NULL ), MD_ARGUMENT_FLAG( "process-html", 0, 1, &opt_process_html, "process MultiMarkdown inside of raw HTML", NULL ),
MD_ARGUMENT_FLAG( "nolabels", 0, 1, &opt_no_labels, "do not look for possible cross-references - improves speed", NULL ),
{ NULL } { NULL }
}; };


#if MD_USE_GET_OPT
char ch; char ch;
while ((ch = getopt_long(argc, argv, "hvo:t:xcbe:", entries, NULL)) != -1) { while ((ch = getopt_long(argc, argv, "hvo:t:xcbe:", entries, NULL)) != -1) {
switch (ch) { switch (ch) {
Expand Down Expand Up @@ -213,30 +176,10 @@ int main(int argc, char * argv[]) {
argc -= optind; argc -= optind;
argv += optind; argv += optind;


// We expect argc and argv to still point just one below the start of remaining args /* We expect argc and argv to still point just one below the start of remaining args */
argc++; argc++;
argv--; argv--;


#else
GError *error = NULL;
GOptionContext *context;
GOptionGroup *ext_group;

context = g_option_context_new ("[FILE...]");
g_option_context_add_main_entries (context, entries, NULL);
ext_group = g_option_group_new ("extensions", "Syntax extensions", "show available syntax extensions", NULL, NULL);
g_option_group_add_entries (ext_group, ext_entries);
g_option_context_add_group (context, ext_group);
g_option_context_set_description (context, "Converts text in specified files (or stdin) from markdown to FORMAT.\n"
"Available FORMATs: html, latex, memoir, beamer, odf, opml");
if (!g_option_context_parse (context, &argc, &argv, &error)) {
g_print ("option parsing failed: %s\n", error->message);
exit (1);
}
g_option_context_free(context);

#endif

/* Process command-line options and arguments. */ /* Process command-line options and arguments. */


if (opt_version) { if (opt_version) {
Expand Down

0 comments on commit 58bdceb

Please sign in to comment.