diff options
author | fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> | 2006-12-28 02:20:09 +0000 |
---|---|---|
committer | fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> | 2006-12-28 02:20:09 +0000 |
commit | a1a30d69bd83e57e494cd3ca5077086077731998 (patch) | |
tree | 8193378eb51d13a20cf9f28ce479af431d3dcba4 /src | |
parent | a1539d9ab8141ab11add5b304792e7c9bc7a363a (diff) | |
download | pandoc-a1a30d69bd83e57e494cd3ca5077086077731998.tar.gz |
+ Removed the convenience symlinks (which don't work on Windows under
Cygwin, due to Windows' lack of true symbolic links).
+ Modified the wrappers to use 'pandoc' instead of the symlinks.
+ Modified the Makefile to remove all references to the symlinks.
+ Removed code from Main.hs that made pandoc's behavior depend on the
name of the calling program.
+ Added code to Main.hs that sets default reader and writer based on
extensions of input and output filenames (if provided). (Thanks to
roktas for the idea.)
+ Modified README and man pages accordingly.
+ Removed WINDOWS-README target from Makefile. It is no longer needed
now that we don't have the symlinks.
git-svn-id: https://pandoc.googlecode.com/svn/trunk@295 788f1e2b-df1e-0410-8736-df70ead52e1b
Diffstat (limited to 'src')
-rw-r--r-- | src/Main.hs | 157 | ||||
-rw-r--r-- | src/wrappers/markdown2pdf.in | 11 | ||||
-rw-r--r-- | src/wrappers/web2markdown.in | 15 |
3 files changed, 98 insertions, 85 deletions
diff --git a/src/Main.hs b/src/Main.hs index 0f8567517..94be551d3 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -45,7 +45,7 @@ import Text.Pandoc.Writers.DefaultHeaders ( defaultHtmlHeader, defaultRTFHeader, defaultS5Header, defaultLaTeXHeader ) import Text.Pandoc.Definition import Text.Pandoc.Shared -import Text.Regex ( mkRegex, splitRegex ) +import Text.Regex ( mkRegex, matchRegex ) import System ( exitWith, getArgs, getProgName ) import System.Exit import System.Console.GetOpt @@ -94,8 +94,8 @@ data Opt = Opt { optPreserveTabs :: Bool -- ^ If @False@, convert tabs to spaces , optTabStop :: Int -- ^ Number of spaces per tab , optStandalone :: Bool -- ^ If @True@, include header, footer - , optReader :: ParserState -> String -> Pandoc -- ^ Read format - , optWriter :: WriterOptions -> Pandoc -> String -- ^ Write fmt + , optReader :: String -- ^ Reader format + , optWriter :: String -- ^ Writer format , optParseRaw :: Bool -- ^ If @True@, parse unconvertable -- HTML and TeX , optCSS :: String -- ^ CSS file to link to @@ -103,64 +103,55 @@ data Opt = Opt , optIncludeBeforeBody :: String -- ^ File to include at top of body , optIncludeAfterBody :: String -- ^ File to include at end of body , optCustomHeader :: String -- ^ Custom header to use, or "DEFAULT" - , optDefaultHeader :: String -- ^ Default header , optTitlePrefix :: String -- ^ Optional prefix for HTML title , optOutputFile :: String -- ^ Name of output file , optNumberSections :: Bool -- ^ If @True@, number sections in LaTeX , optIncremental :: Bool -- ^ If @True@, incremental lists in S5 , optSmart :: Bool -- ^ If @True@, use smart typography , optASCIIMathML :: Bool -- ^ If @True@, use ASCIIMathML in HTML - , optShowUsage :: Bool -- ^ If @True@, show usage message , optDebug :: Bool -- ^ If @True@, output debug messages } -- | Defaults for command-line options. -startOpt :: Opt -startOpt = Opt +defaultOpts :: Opt +defaultOpts = Opt { optPreserveTabs = False , optTabStop = 4 , optStandalone = False - , optReader = readMarkdown - , optWriter = writeHtml + , optReader = "" -- null for default reader + , optWriter = "" -- null for default writer , optParseRaw = False , optCSS = "" , optIncludeInHeader = "" , optIncludeBeforeBody = "" , optIncludeAfterBody = "" , optCustomHeader = "DEFAULT" - , optDefaultHeader = defaultHtmlHeader , optTitlePrefix = "" , optOutputFile = "" -- null for stdout , optNumberSections = False , optIncremental = False , optSmart = False , optASCIIMathML = False - , optShowUsage = False , optDebug = False } --- | A list of functions, each transforming the options data structure in response --- to a command-line option. -allOptions :: [OptDescr (Opt -> IO Opt)] -allOptions = +-- | A list of functions, each transforming the options data structure +-- in response to a command-line option. +options :: [OptDescr (Opt -> IO Opt)] +options = [ Option "fr" ["from","read"] (ReqArg - (\arg opt -> case (lookup (map toLower arg) readers) of - Just reader -> return opt { optReader = reader } - Nothing -> error ("Unknown reader: " ++ arg) ) + (\arg opt -> return opt { optReader = map toLower arg }) "FORMAT") - ("Source format (" ++ - (concatMap (\(name, fn) -> " " ++ name) readers) ++ " )") + ("Input format (" ++ (joinWithSep ", " (map fst readers)) ++ + ")") , Option "tw" ["to","write"] (ReqArg - (\arg opt -> case (lookup (map toLower arg) writers) of - Just (writer, defaultHeader) -> - return opt { optWriter = writer, - optDefaultHeader = defaultHeader } - Nothing -> error ("Unknown writer: " ++ arg) ) + (\arg opt -> return opt { optWriter = map toLower arg }) "FORMAT") - ("Output format (" ++ (concatMap (\(name, fn) -> " " ++ name) writers) ++ " )") + ("Output format (" ++ (joinWithSep ", " (map fst writers)) ++ + ")") , Option "s" ["standalone"] (NoArg @@ -169,8 +160,7 @@ allOptions = , Option "o" ["output"] (ReqArg - (\arg opt -> do - return opt { optOutputFile = arg }) + (\arg opt -> return opt { optOutputFile = arg }) "FILENAME") "Name of output file" @@ -286,57 +276,66 @@ allOptions = , Option "h" ["help"] (NoArg - (\opt -> return opt { optShowUsage = True })) + (\_ -> do + prg <- getProgName + hPutStr stderr (reformatUsageInfo $ + usageInfo (prg ++ " [OPTIONS] [FILES]") options) + exitWith $ ExitFailure 2)) "Show help" ] --- parse name of calling program and return default reader and writer descriptions -parseProgName name = - case (splitRegex (mkRegex "2") (map toLower name)) of - [from, to] -> (from, to) - _ -> ("markdown", "html") - --- set default options based on reader and writer descriptions; start is starting options -setDefaultOpts from to start = - case ((lookup from readers), (lookup to writers)) of - (Just reader, Just (writer, header)) -> start {optReader = reader, - optWriter = writer, - optDefaultHeader = header} - _ -> start - --- True if single-letter option is in option list -inOptList :: [Char] -> OptDescr (Opt -> IO Opt) -> Bool -inOptList list desc = - let (Option letters _ _ _) = desc in - any (\x -> x `elem` list) letters - -- Reformat usage message so it doesn't wrap illegibly +reformatUsageInfo :: String -> String reformatUsageInfo = gsub " *--" " --" . gsub "(-[A-Za-z0-9]) *--" "\\1, --" . gsub " *([^- ])" "\n\t\\1" -main = do - - name <- getProgName - let (from, to) = parseProgName name +-- Determine default reader based on source file extensions +defaultReaderName :: [String] -> String +defaultReaderName [] = "markdown" +defaultReaderName (x:xs) = + let x' = map toLower x in + case (matchRegex (mkRegex ".*\\.(.*)") x') of + Nothing -> defaultReaderName xs -- no extension + Just ["xhtml"] -> "html" + Just ["html"] -> "html" + Just ["htm"] -> "html" + Just ["tex"] -> "latex" + Just ["latex"] -> "latex" + Just ["ltx"] -> "latex" + Just ["rst"] -> "rst" + Just ["native"] -> "native" + Just _ -> "markdown" + +-- Determine default writer based on output file extension +defaultWriterName :: String -> String +defaultWriterName "" = "html" -- no output file +defaultWriterName x = + let x' = map toLower x in + case (matchRegex (mkRegex ".*\\.(.*)") x') of + Nothing -> "markdown" -- no extension + Just [""] -> "markdown" -- empty extension + Just ["tex"] -> "latex" + Just ["latex"] -> "latex" + Just ["ltx"] -> "latex" + Just ["rtf"] -> "rtf" + Just ["rst"] -> "rst" + Just ["s5"] -> "s5" + Just ["native"] -> "native" + Just ["txt"] -> "markdown" + Just ["text"] -> "markdown" + Just ["md"] -> "markdown" + Just ["markdown"] -> "markdown" + Just _ -> "html" - let irrelevantOptions = if not ('2' `elem` name) - then "" - else "frtwD" ++ - (if (to /= "html" && to /= "s5") then "SmcT" else "") ++ - (if (to /= "latex") then "N" else "") ++ - (if (to /= "s5") then "i" else "") ++ - (if (from /= "html" && from /= "latex") then "R" else "") - - let options = filter (not . inOptList irrelevantOptions) allOptions - - let defaultOpts = setDefaultOpts from to startOpt +main = do args <- getArgs let (actions, sources, errors) = getOpt Permute options args if (not (null errors)) then do + name <- getProgName mapM (\e -> hPutStrLn stderr e) errors hPutStrLn stderr (reformatUsageInfo $ usageInfo (name ++ " [OPTIONS] [FILES]") options) @@ -350,30 +349,39 @@ main = do let Opt { optPreserveTabs = preserveTabs , optTabStop = tabStop , optStandalone = standalone - , optReader = reader - , optWriter = writer + , optReader = readerName + , optWriter = writerName , optParseRaw = parseRaw , optCSS = css , optIncludeInHeader = includeHeader , optIncludeBeforeBody = includeBefore , optIncludeAfterBody = includeAfter , optCustomHeader = customHeader - , optDefaultHeader = defaultHeader , optTitlePrefix = titlePrefix , optOutputFile = outputFile , optNumberSections = numberSections , optIncremental = incremental , optSmart = smart , optASCIIMathML = asciiMathML - , optShowUsage = showUsage , optDebug = debug } = opts - if showUsage - then do - hPutStr stderr (reformatUsageInfo $ usageInfo (name ++ " [OPTIONS] [FILES]") options) - exitWith $ ExitFailure 2 - else return () + -- assign reader and writer based on options and filenames + let readerName' = if null readerName + then defaultReaderName sources + else readerName + + let writerName' = if null writerName + then defaultWriterName outputFile + else writerName + + reader <- case (lookup readerName' readers) of + Just r -> return r + Nothing -> error ("Unknown reader: " ++ readerName') + + (writer, defaultHeader) <- case (lookup writerName' writers) of + Just (w,h) -> return (w, h) + Nothing -> error ("Unknown writer: " ++ writerName') output <- if ((null outputFile) || debug) then return stdout @@ -385,7 +393,6 @@ main = do hPutStr stderr $ concatMap (\s -> "INPUT=" ++ s ++ "\n") sources else return () - let writingS5 = (defaultHeader == defaultS5Header) let tabFilter = if preserveTabs then id else (tabsToSpaces tabStop) let addBlank str = str ++ "\n\n" let removeCRs str = filter (/= '\r') str -- remove DOS-style line endings @@ -407,7 +414,7 @@ main = do writerTitlePrefix = titlePrefix, writerSmart = smart, writerTabStop = tabStop, - writerS5 = writingS5, + writerS5 = (writerName=="s5"), writerIncremental = incremental, writerNumberSections = numberSections, writerIncludeBefore = includeBefore, diff --git a/src/wrappers/markdown2pdf.in b/src/wrappers/markdown2pdf.in index c222c1cbd..71d58a7cd 100644 --- a/src/wrappers/markdown2pdf.in +++ b/src/wrappers/markdown2pdf.in @@ -1,6 +1,6 @@ #!/bin/sh -e -REQUIRED="markdown2latex pdflatex" +REQUIRED="pdflatex" ### common.sh @@ -9,9 +9,12 @@ REQUIRED="markdown2latex pdflatex" texname=output logfile=$THIS_TEMPDIR/log -if ! markdown2latex -s -d "$@" >$THIS_TEMPDIR/$texname.tex 2>$logfile; then - [ -f $logfile ] && sed -e 's/markdown2latex/markdown2pdf/g' \ - -e '/^INPUT=/d' -e '/^OUTPUT=/d' $logfile >&2 +if ! pandoc -s -d -r markdown -w latex "$@" >$THIS_TEMPDIR/$texname.tex \ +2>$logfile; then + [ -f $logfile ] && sed -e 's/^pandoc/markdown2pdf/g' \ + -e '/^INPUT=/d' -e '/^OUTPUT=/d' \ + -e '/^[[:space:]]*\(-f\|-t\|-s\|-R\|-S\|-m\|-i\|-c\|-T\|-D\|-d\)/,/./d'\ + -e 's/(implies -s)//g' $logfile >&2 exit 1 fi diff --git a/src/wrappers/web2markdown.in b/src/wrappers/web2markdown.in index 64ff3db9b..89e884c3d 100644 --- a/src/wrappers/web2markdown.in +++ b/src/wrappers/web2markdown.in @@ -2,7 +2,7 @@ # converts HTML from a URL, file, or stdin to markdown # uses an available program to fetch URL and tidy to normalize it first -REQUIRED="tidy html2markdown" +REQUIRED="tidy" ### common.sh @@ -72,14 +72,16 @@ grabber= while [ $# -gt 0 ]; do case "$1" in -h|--help) - html2markdown -h 2>&1 | sed -e 's/html2markdown/web2markdown/' 1>&2 + pandoc -h 2>&1 | sed -e 's/pandoc/web2markdown/' \ + -e '/^[[:space:]]*\(-f\|-t\|-S\|-N\|-m\|-i\|-c\|-T\|-D\|-d\)/,/./d'\ + 1>&2 err " -e ENCODING, --encoding=ENCODING" err " Specify character encoding of input" err " -g COMMAND, --grabber=COMMAND" err " Specify command to be used to grab contents of URL" exit 0 ;; -v|--version) - html2markdown -v + pandoc -v 2>&1 | sed -e 's/pandoc/web2markdown/' 1>&2 exit 0 ;; -e) shift @@ -112,7 +114,7 @@ while [ $# -gt 0 ]; do shift done -# Unpack options. Now "$@" will hold the html2markdown options. +# Unpack options. Now "$@" will hold the pandoc options. oldifs="$IFS"; IFS="$NEWLINE"; set -- $options; IFS="$oldifs" inurl= @@ -162,10 +164,11 @@ else # assume UTF-8 fi if [ -z "$argument" ]; then - tidy -utf8 2>/dev/null | html2markdown "$@" + tidy -utf8 2>/dev/null | pandoc -r html -w markdown "$@" else if [ -f "$argument" ]; then - to_utf8 "$argument" | tidy -utf8 2>/dev/null | html2markdown "$@" + to_utf8 "$argument" | + tidy -utf8 2>/dev/null | pandoc -r html -w markdown "$@" else err "File '$argument' not found." exit 1 |