Permalink
Browse files

initial import

  • Loading branch information...
0 parents commit 7a083c5daec58164ab95316a53013dd2521225cc @knieriem committed Nov 21, 2010
Showing with 1,879 additions and 0 deletions.
  1. +14 −0 .hgignore
  2. +78 −0 LICENSE
  3. +69 −0 Makefile
  4. +214 −0 README.peg-markdown
  5. +15 −0 cmd/Makefile
  6. +41 −0 cmd/main.go
  7. +25 −0 doc.go
  8. +138 −0 markdown.go
  9. +37 −0 misc/c2go.sed
  10. +16 −0 misc/devel.mk
  11. +51 −0 misc/gofmt.rc
  12. +229 −0 output.go
  13. +952 −0 parser.leg
@@ -0,0 +1,14 @@
+# use glob syntax.
+syntax: glob
+
+parser.leg.go
+cmd/markdown
+orig-c-src
+core
++*
+*.orig
+[58].*
+*.[58]
+,*
+*~
+[#]*
@@ -0,0 +1,78 @@
+markdown in Go, implemented using PEG grammar
+
+Copyright (c) 2010 Michael Teichgräber
+
+This is a translation of peg-markdown, written
+by John MacFarlane, into Go:
+
+Copyright (c) 2008 John MacFarlane
+
+peg-markdown is released under both the GPL and MIT licenses.
+You may pick the license that best fits your needs.
+
+The GPL
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+The MIT License
+
+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.
+
+* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+
+peg (http://github.com/pointlander/peg),
+ based on http://piumarta.com/software/peg/,
+written by Andrew J Snodgrass.
+
+Modifications to support LE grammars by Michael Teichgräber
+
+Copyright (c) 2010, Go Authors
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation and/or
+ other materials provided with the distribution.
+ * Neither the name of the Go Authors nor the names of its contributors may be used to
+ endorse or promote products derived from this software without specific prior written permission.
+
+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 HOLDER 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.
@@ -0,0 +1,69 @@
+include $(GOROOT)/src/Make.inc
+
+TARG=markdown
+GOFILES=\
+ markdown.go\
+ output.go\
+ parser.leg.go\
+
+package:
+
+include $(GOROOT)/src/Make.pkg
+
+all: cmd
+
+#
+# mdtest runs MarkdownTests-1.0.3 that come with original C sources
+#
+mdtest: package cmd orig-c-src
+ make -C cmd test
+
+cmd: package
+ make -C cmd
+
+
+CLEANFILES=\
+ parser.leg.go\
+ _obj\
+ ,,c\
+ ,,fmt\
+
+distclean: clean clean-sub
+ rm -rf orig-c-src
+
+clean-sub:
+ for dir in cmd peg peg/leg; do make -C $$dir clean; done
+
+
+#
+# LEG parser generator stuff
+#
+LEG = ./peg/leg/leg
+%.leg.go: %.leg $(LEG)
+ $(LEG) $<
+
+$(LEG):
+ make -C peg all
+ make -C peg/leg all
+
+peg:
+
+
+#
+# access to original C source files
+#
+VCS = git
+# also, if hggit extension is available:
+# VCS = hg
+
+orig-c-src:
+ $(VCS) clone git://github.com/jgm/peg-markdown.git $@
+
+
+
+include misc/devel.mk
+
+.PHONY: \
+ cmd\
+ distclean\
+ mdtest\
@@ -0,0 +1,214 @@
+What is this?
+=============
+
+This is an implementation of John Gruber's [markdown][] in C. It uses a
+[parsing expression grammar (PEG)][] to define the syntax. This should
+allow easy modification and extension. It currently supports output in
+HTML, LaTeX, or groff_mm formats, and adding new formats is relatively
+easy.
+
+[parsing expression grammar (PEG)]: http://en.wikipedia.org/wiki/Parsing_expression_grammar
+[markdown]: http://daringfireball.net/projects/markdown/
+
+It is pretty fast. A 179K text file that takes 5.7 seconds for
+Markdown.pl (v. 1.0.1) to parse takes less than 0.2 seconds for this
+markdown. It does, however, use a lot of memory (up to 4M of heap space
+while parsing the 179K file, and up to 80K for a 4K file). (Note that
+the memory leaks in earlier versions of this program have now been
+plugged.)
+
+Both a library and a standalone program are provided.
+
+peg-markdown is written and maintained by John MacFarlane (jgm on
+github), with significant contributions by Ryan Tomayko (rtomayko).
+It is released under both the GPL and the MIT license; see LICENSE for
+details.
+
+Installing
+==========
+
+On a linux or unix-based system
+-------------------------------
+
+This program is written in portable ANSI C. It requires
+[glib2](http://www.gtk.org/download.html). Most *nix systems will have
+this installed already. The build system requires GNU make.
+
+The other required dependency, [Ian Piumarta's peg/leg PEG parser
+generator](http://piumarta.com/software/peg/), is included in the source
+directory. It will be built automatically. (However, it is not as portable
+as peg-markdown itself, and seems to require gcc.)
+
+To make the 'markdown' executable:
+
+ make
+
+(Or, on some systems, `gmake`.) Then, for usage instructions:
+
+ ./markdown --help
+
+To run John Gruber's Markdown 1.0.3 test suite:
+
+ make test
+
+The test suite will fail on one of the list tests. Here's why.
+Markdown.pl encloses "item one" in the following list in `<p>` tags:
+
+ 1. item one
+ * subitem
+ * subitem
+
+ 2. item two
+
+ 3. item three
+
+peg-markdown does not enclose "item one" in `<p>` tags unless it has a
+following blank line. This is consistent with the official markdown
+syntax description, and lets the author of the document choose whether
+`<p>` tags are desired.
+
+Cross-compiling for Windows with MinGW on a linux box
+-----------------------------------------------------
+
+Prerequisites:
+
+* Linux system with MinGW cross compiler For Ubuntu:
+
+ sudo apt-get install mingw32
+
+* [Windows glib-2.0 binary & development files](http://www.gtk.org/download-windows.html).
+ Unzip files into cross-compiler directory tree (e.g., `/usr/i586-mingw32msvc`).
+
+Steps:
+
+1. Create the markdown parser using Linux-compiled `leg` from peg-0.1.4:
+
+ ./peg-0.1.4/leg markdown_parser.leg >markdown_parser.c
+
+ (Note: The same thing could be accomplished by cross-compiling leg,
+ executing it on Windows, and copying the resulting C file to the Linux
+ cross-compiler host.)
+
+2. Run the cross compiler with include flag for the Windows glib-2.0 headers:
+ for example,
+
+ /usr/bin/i586-mingw32msvc-cc -c \
+ -I/usr/i586-mingw32msvc/include/glib-2.0 \
+ -I/usr/i586-mingw32msvc/lib/glib-2.0/include -Wall -O3 -ansi markdown*.c
+
+3. Link against Windows glib-2.0 headers: for example,
+
+ /usr/bin/i586-mingw32msvc-cc markdown*.o \
+ -Wl,-L/usr/i586-mingw32msvc/lib/glib,--dy,--warn-unresolved-symbols,-lglib-2.0 \
+ -o markdown.exe
+
+The resulting executable depends on the glib dll file, so be sure to
+load the glib binary on the Windows host.
+
+Compiling with MinGW on Windows
+-------------------------------
+
+These directions assume that MinGW is installed in `c:\MinGW` and glib-2.0
+is installed in the MinGW directory hierarchy (with the mingw bin directory
+in the system path).
+
+Unzip peg-markdown in a temp directory. From the directory with the
+peg-markdown source, execute:
+
+ cd peg-0.1.4
+ for %i in (*.c) do @gcc -g -Wall -O3 -DNDEBUG -c -o %~ni.o %i
+ gcc -o leg.exe leg.o tree.o compile.o
+ cd ..
+ peg-0.1.4\leg.exe markdown_parser.leg >markdown_parser.c
+ @for %i in (markdown*.c) do @gcc -mms-bitfields -Ic:/MinGW/include/glib-2.0 -Ic:/MinGW/lib/glib-2.0/include -c -o %~ni.o %i
+ gcc -O3 -Lc:/MinGW/lib/glib-2.0 -lglib-2.0 -lintl markdown.o markdown_lib.o markdown_output.o markdown_parser.o -o markdown.exe -Wl,--dy,--warn-unresolved-symbols,-lglib-2.0,-Lc:/MinGW/lib/glib-2.0,-lglib-2.0,-lintl
+
+(Windows instructions courtesy of Matt Wolf.)
+
+Extensions
+==========
+
+peg-markdown supports extensions to standard markdown syntax.
+These can be turned on using the command line flag `-x` or
+`--extensions`. `-x` by itself turns on all extensions. Extensions
+can also be turned on selectively, using individual command-line
+options. To see the available extensions:
+
+ ./markdown --help-extensions
+
+The `--smart` extension provides "smart quotes", dashes, and ellipses.
+
+The `--notes` extension provides a footnote syntax like that of
+Pandoc or PHP Markdown Extra.
+
+Using the library
+=================
+
+The library exports two functions:
+
+ GString * markdown_to_g_string(char *text, int extensions, int output_format);
+ char * markdown_to_string(char *text, int extensions, int output_format);
+
+The only difference between these is that `markdown_to_g_string` returns a
+`GString` (glib's automatically resizable string), while `markdown_to_string`
+returns a regular character pointer. The memory allocated for these must be
+freed by the calling program, using `g_string_free()` or `free()`.
+
+`text` is the markdown-formatted text to be converted. Note that tabs will
+be converted to spaces, using a four-space tab stop. Character encodings are
+ignored.
+
+`extensions` is a bit-field specifying which syntax extensions should be used.
+If `extensions` is 0, no extensions will be used. If it is `0xFFFFFF`,
+all extensions will be used. To set extensions selectively, use the
+bitwise `&` operator and the following constants:
+
+ - `EXT_SMART` turns on smart quotes, dashes, and ellipses.
+ - `EXT_NOTES` turns on footnote syntax. [Pandoc's footnote syntax][] is used here.
+ - `EXT_FILTER_HTML` filters out raw HTML (except for styles).
+ - `EXT_FILTER_STYLES` filters out styles in HTML.
+
+ [Pandoc's footnote syntax]: http://johnmacfarlane.net/pandoc/README.html#footnotes
+
+`output_format` is either `HTML_FORMAT`, `LATEX_FORMAT`, or `GROFF_MM_FORMAT`.
+
+To use the library, include `markdown_lib.h`. See `markdown.c` for an example.
+
+Hacking
+=======
+
+It should be pretty easy to modify the program to produce other formats
+than HTML or LaTeX, and to parse syntax extensions. A quick guide:
+
+ * `markdown_parser.leg` contains the grammar itself.
+
+ * `markdown_output.c` contains functions for printing the `Element`
+ structure in various output formats.
+
+ * To add an output format, add the format to `markdown_formats` in
+ `markdown_lib.h`. Then modify `print_element` in `markdown_output.c`,
+ and add functions `print_XXXX_string`, `print_XXXX_element`, and
+ `print_XXXX_element_list`. Also add an option in the main program
+ that selects the new format. Don't forget to add it to the list of
+ formats in the usage message.
+
+ * To add syntax extensions, define them in the PEG grammar
+ (`markdown_parser.leg`), using existing extensions as a guide. New
+ inline elements will need to be added to `Inline =`; new block
+ elements will need to be added to `Block =`. (Note: the order
+ of the alternatives does matter in PEG grammars.)
+
+ * If you need to add new types of elements, modify the `keys`
+ enum in `markdown_peg.h`.
+
+ * By using `&{ }` rules one can selectively disable extensions
+ depending on command-line options. For example,
+ `&{ extension(EXT_SMART) }` succeeds only if the `EXT_SMART` bit
+ of the global `syntax_extensions` is set. Add your option to
+ `markdown_extensions` in `markdown_lib.h`, and add an option in
+ `markdown.c` to turn on your extension.
+
+ * Note: Avoid using `[^abc]` character classes in the grammar, because
+ they cause problems with non-ascii input. Instead, use: `( !'a' !'b'
+ !'c' . )`
+
@@ -0,0 +1,15 @@
+include $(GOROOT)/src/Make.inc
+
+TARG=markdown
+GOFILES=\
+ main.go\
+
+R = ..
+PREREQ += $(R)/_obj/markdown.a
+
+include $(GOROOT)/src/Make.cmd
+
+
+test: $(TARG)
+ cd $(R)/orig-c-src/MarkdownTest_1.0.3; \
+ ./MarkdownTest.pl --script=../../cmd/$< --tidy
Oops, something went wrong. Retry.

0 comments on commit 7a083c5

Please sign in to comment.