git.iko.soy/typeable/pandoc
on master help sign in
Text.Pandoc.Options: modifications for image attributes.

* Added `Ext_common_link_attributes` constructor to `Extension` (for link and image attributes). * Added this to `pandocExtensions` and `phpMarkdownExtraExtensions`. * Added `writerDpi` to `WriterOptions`. * pandoc.hs: Added `--dpi` option. * Updated README for `--dpi` and `common_link_attributes` extension. Patch due to mb21, with some modifications: `writerDpi` is now an `Int` rather than a `Double`.

commit5df099957e0ed252a4d36161fc6c4ce7b18f528b authorJohn MacFarlane <jgm@berkeley.edu> dateWed 1 Apr 2015, 15:52 (12 years ago) parent2e80643
3 files changed+88−7
README+67−5 pandoc.hs+16−2 src/Text/Pandoc/Options.hs+5
README+67−5
@@ -420,14 +420,22 @@ General writer options
420420: Print a system default data file. Files in the user data directory
421421 are ignored.
422422
423+`--dpi`=*NUMBER*
424+: Specify the dpi (dots per inch) value for conversion from pixels
425+ to inch/centimeters and vice versa. The default is 96dpi.
426+ Technically, the correct term would be ppi (pixels per inch).
427+
423428`--no-wrap`
424429
425430: Disable text wrapping in output. By default, text is wrapped
426− appropriately for the output format.
431+ appropriately for the output format. This affects only the
432+ generated source code, not the layout on the rendered page.
427433
428434`--columns=`*NUMBER*
429435
430436: Specify length of lines in characters (for text wrapping).
437+ This affects only the generated source code, not the layout on
438+ the rendered page.
431439
432440`--toc`, `--table-of-contents`
433441
@@ -2634,6 +2642,53 @@ nonbreaking space after the image:
26342642
26352643 ![This image won't be a figure](/url/of/image.png)\
26362644
2645+#### Extension: `common_link_attributes` ####
2646+
2647+Attributes can be set on images:
2648+
2649+ An inline ![image](foo.jpg){#id .class width=30 height=20px}
2650+ and a reference ![image][ref] with attributes.
2651+
2652+ [ref]: foo.jpg "optional title" {#id .class key=val key2="val 2"}
2653+
2654+(This syntax is compatible with [PHP Markdown Extra] when only `#id`
2655+and `.class` are used.)
2656+
2657+For HTML and EPUB, all attributes except `width` and `height` (but
2658+including `srcset` and `sizes`) are passed through as is. The other
2659+writers ignore attributes that are not supported by their output
2660+format.
2661+
2662+The `width` and `height` attributes on images are treated specially. When
2663+used without a unit, the unit is assumed to be pixels. However, any of
2664+the following unit identifiers can be used: `px`, `cm`, `mm`, `in`, `inch`
2665+and `%`. There must not be any spaces between the number and the unit.
2666+For example:
2667+
2668+```
2669+![](file.jpg){width=50%}
2670+```
2671+
2672+- Dimensions are converted to inches for output in page-based formats like
2673+ LaTeX. Dimensions are converted to pixels for output in HTML-like
2674+ formats. Use the `--dpi` option to specify the number of pixels per
2675+ inch. The default is 96dpi.
2676+- The `%` unit is generally relative to some available space.
2677+ For example the above example will render to
2678+ `<img href="file.jpg" style="width: 50%;" />` (HTML),
2679+ `\includegraphics[width=0.5\textwidth]{file.jpg}` (LaTeX), or
2680+ `\externalfigure[file.jpg][width=0.5\textwidth]` (ConTeXt).
2681+- Some output formats have a notion of a class
2682+ ([ConTeXt](http://wiki.contextgarden.net/Using_Graphics#Multiple_Image_Settings))
2683+ or a unique identifier (LaTeX `\caption`), or both (HTML).
2684+- When no `width` or `height` attributes are specified, the fallback
2685+ is to look at the image resolution and the dpi metadata embedded in
2686+ the image file.
2687+
2688+Note that while attributes are also parsed on links, pandoc's internal
2689+document model provides nowhere to put them, so they are presently
2690+just ignored.
2691+
26372692
26382693Footnotes
26392694---------
@@ -2908,9 +2963,15 @@ letters are omitted.
29082963
29092964#### Extension: `link_attributes` ####
29102965
2911−Parses multimarkdown style key-value attributes on link and image references.
2912−Note that pandoc's internal document model provides nowhere to put
2913−these, so they are presently just ignored.
2966+Parses multimarkdown style key-value attributes on link
2967+and image references. (Since pandoc's internal document model
2968+provides nowhere to put these for links, they are presently just
2969+ignored, but they work for images.)
2970+
2971+ This is a reference ![image][ref] with multimarkdown attributes.
2972+
2973+ [ref]: http://path.to/image "Image title" width=20px height=30px
2974+ id=myId class="myClass1 myClass2"
29142975
29152976#### Extension: `mmd_header_identifiers` ####
29162977
@@ -2953,7 +3014,8 @@ variants are supported:
29533014`markdown_phpextra` (PHP Markdown Extra)
29543015: `footnotes`, `pipe_tables`, `raw_html`, `markdown_attribute`,
29553016 `fenced_code_blocks`, `definition_lists`, `intraword_underscores`,
2956− `header_attributes`, `abbreviations`, `shortcut_reference_links`.
3017+ `header_attributes`, `common_link_attributes`, `abbreviations`,
3018+ `shortcut_reference_links`.
29573019
29583020`markdown_github` (GitHub-flavored Markdown)
29593021: `pipe_tables`, `raw_html`, `tex_math_single_backslash`,
pandoc.hs+16−2
@@ -196,6 +196,7 @@ data Opt = Opt
196196 , optIgnoreArgs :: Bool -- ^ Ignore command-line arguments
197197 , optVerbose :: Bool -- ^ Verbose diagnostic output
198198 , optReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst
199+ , optDpi :: Int -- ^ Dpi
199200 , optWrapText :: Bool -- ^ Wrap text
200201 , optColumns :: Int -- ^ Line length in characters
201202 , optFilters :: [FilePath] -- ^ Filters to apply
@@ -258,6 +259,7 @@ defaultOpts = Opt
258259 , optIgnoreArgs = False
259260 , optVerbose = False
260261 , optReferenceLinks = False
262+ , optDpi = 96
261263 , optWrapText = True
262264 , optColumns = 72
263265 , optFilters = []
@@ -454,6 +456,16 @@ options =
454456 "FILE")
455457 "" -- "Print default data file"
456458
459+ , Option "" ["dpi"]
460+ (ReqArg
461+ (\arg opt ->
462+ case safeRead arg of
463+ Just t | t > 0 -> return opt { optDpi = t }
464+ _ -> err 31
465+ "dpi must be a number greater than 0")
466+ "NUMBER")
467+ "" -- "Dpi (default 96)"
468+
457469 , Option "" ["no-wrap"]
458470 (NoArg
459471 (\opt -> return opt { optWrapText = False }))
@@ -1012,8 +1024,8 @@ extractMedia media dir d =
10121024 return $ walk (adjustImagePath dir fps) d
10131025
10141026adjustImagePath :: FilePath -> [FilePath] -> Inline -> Inline
1015−adjustImagePath dir paths (Image lab (src, tit))
1016− | src `elem` paths = Image lab (dir ++ "/" ++ src, tit)
1027+adjustImagePath dir paths (Image attr lab (src, tit))
1028+ | src `elem` paths = Image attr lab (dir ++ "/" ++ src, tit)
10171029adjustImagePath _ _ x = x
10181030
10191031adjustMetadata :: M.Map String MetaValue -> Pandoc -> IO Pandoc
@@ -1087,6 +1099,7 @@ main = do
10871099 , optIgnoreArgs = ignoreArgs
10881100 , optVerbose = verbose
10891101 , optReferenceLinks = referenceLinks
1102+ , optDpi = dpi
10901103 , optWrapText = wrap
10911104 , optColumns = columns
10921105 , optFilters = filters
@@ -1304,6 +1317,7 @@ main = do
13041317 writerNumberOffset = numberFrom,
13051318 writerSectionDivs = sectionDivs,
13061319 writerReferenceLinks = referenceLinks,
1320+ writerDpi = dpi,
13071321 writerWrapText = wrap,
13081322 writerColumns = columns,
13091323 writerEmailObfuscation = obfuscationMethod,
src/Text/Pandoc/Options.hs+5
@@ -86,6 +86,7 @@ data Extension =
8686 | Ext_markdown_attribute -- ^ Interpret text inside HTML as markdown
8787 -- iff container has attribute 'markdown'
8888 | Ext_escaped_line_breaks -- ^ Treat a backslash at EOL as linebreak
89+ | Ext_common_link_attributes -- ^ link and image attributes
8990 | Ext_link_attributes -- ^ MMD style reference link attributes
9091 | Ext_autolink_bare_uris -- ^ Make all absolute URIs into links
9192 | Ext_fancy_lists -- ^ Enable fancy list numbers and delimiters
@@ -154,6 +155,7 @@ pandocExtensions = Set.fromList
154155 , Ext_subscript
155156 , Ext_auto_identifiers
156157 , Ext_header_attributes
158+ , Ext_common_link_attributes
157159 , Ext_implicit_header_references
158160 , Ext_line_blocks
159161 , Ext_shortcut_reference_links
@@ -187,6 +189,7 @@ phpMarkdownExtraExtensions = Set.fromList
187189 , Ext_definition_lists
188190 , Ext_intraword_underscores
189191 , Ext_header_attributes
192+ , Ext_common_link_attributes
190193 , Ext_abbreviations
191194 , Ext_shortcut_reference_links
192195 ]
@@ -324,6 +327,7 @@ data WriterOptions = WriterOptions
324327 , writerSectionDivs :: Bool -- ^ Put sections in div tags in HTML
325328 , writerExtensions :: Set Extension -- ^ Markdown extensions that can be used
326329 , writerReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst
330+ , writerDpi :: Int -- ^ Dpi for pixel to/from inch/cm conversions
327331 , writerWrapText :: Bool -- ^ Wrap text to line length
328332 , writerColumns :: Int -- ^ Characters in a line (for text wrapping)
329333 , writerEmailObfuscation :: ObfuscationMethod -- ^ How to obfuscate emails
@@ -370,6 +374,7 @@ instance Default WriterOptions where
370374 , writerSectionDivs = False
371375 , writerExtensions = pandocExtensions
372376 , writerReferenceLinks = False
377+ , writerDpi = 96
373378 , writerWrapText = True
374379 , writerColumns = 72
375380 , writerEmailObfuscation = JavascriptObfuscation