Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline link style option #23

Merged
merged 1 commit into from
May 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ In the browser:
<h1>Hello, World!</h1>
<p>My tasks for today:</p>
<ul>
<li>Learn all about <a href="http://neocotic.com/html-md">html.md</a></li>
<li>Learn all about <a href="http://neocotic.com/html.md">html.md</a></li>
<li>Tell everyone how <strong>awesome</strong> it is!</li>
</ul>
</body>
Expand All @@ -76,8 +76,8 @@ In the terminal:

``` bash
# provide HTML to be converted and print it back out to stdout:
$ md -ep "I <i>love</i> <b>html.md</b>"
I _love_ **html.md**
$ md -epi "I <b>love</b> <a href='http://neocotic.com/html.md'>html.md</a>"
I **love** [html.md](http://neocotic.com/html.md)
# convert HTML files and output them into another directory:
$ md -o ./markdown ./html/*.html
# convert all HTML files in the current directory into Markdown files:
Expand All @@ -95,6 +95,7 @@ $ md -l .
-a, --absolute always use absolute URLs for links
-d, --debug print additional debug information
-e, --eval pass a string from the command line as input
-i, --inline generate inline style links
-l, --long-ext use long extension for Markdown files
-o, --output <dir> set the output directory for converted Markdown
-p, --print print out the converted Markdown
Expand Down Expand Up @@ -127,6 +128,10 @@ The following options are recognised by this method (all of which are optional);
<td>debug</td>
<td>Prepends additional debug information to the Markdown output</td>
</tr>
<tr>
<td>inline</td>
<td>All links are generated using the inline style</td>
</tr>
</table>

### Miscellaneous
Expand Down
2 changes: 1 addition & 1 deletion dist/md.min.js

Large diffs are not rendered by default.

29 changes: 15 additions & 14 deletions docs/command.html

Large diffs are not rendered by default.

51 changes: 28 additions & 23 deletions docs/md.html

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions lib/command.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 10 additions & 14 deletions lib/md.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/md.1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

.B md
[
.B \-adlp
.B \-adilp
]
[
.B \-o
Expand Down Expand Up @@ -54,6 +54,10 @@ to print debugging messages about its progress.
.br
Specifies the HTML string to be evaluated.

.TP
.B \-i/\-\-inline
Always generate Markdown links using inline style.

.TP
.B \-l/\-\-long-ext
Generated Markdown files should have the long file
Expand Down
5 changes: 4 additions & 1 deletion src/command.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ parsePath = (source, topLevel, base) ->
# Parse the HTML `input` and write it out accordingly.
parseHtml = (file, input, base) ->
try
# Extract only relevant options from `program`.
{absolute, debug, inline} = program
# Let `md` work its magic on the HTML `input`.
output = md input, program
output = md input, {absolute, debug, inline}

# Either write the output to `stdout` or to a corresponding Markdown file depending on whether
# the `print` option was enabled.
Expand All @@ -124,6 +126,7 @@ parseOptions = ->
.option('-a, --absolute', 'always use absolute URLs for links')
.option('-d, --debug', 'print additional debug information')
.option('-e, --eval', 'pass a string from the command line as input')
.option('-i, --inline', 'generate inline style links')
.option('-l, --long-ext', 'use long extension for Markdown files')
.option('-o, --output <dir>', 'set the output directory for converted Markdown')
.option('-p, --print', 'print out the converted Markdown')
Expand Down
37 changes: 23 additions & 14 deletions src/md.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

# Default option values.
DEFAULT_OPTIONS =
absolute: off
debug: off
absolute: no
debug: no
inline: no
# Save the previous value of the global `md` variable for *noConflict* mode.
PREVIOUS_MD = @md
# Map of replacement strings for *special* Markdown characters.
Expand Down Expand Up @@ -157,7 +158,7 @@ class HtmlParser

# Copy all default option values across to `options` only where they were not specified.
for own key, defaultValue of DEFAULT_OPTIONS
@options[key] = defaultValue unless @options.hasOwnProperty key
@options[key] = defaultValue if typeof @options[key] is 'undefined'

# Append `str` to the buffer string.
append: (str) ->
Expand Down Expand Up @@ -441,33 +442,41 @@ class HtmlParser
# Links with no URLs are treated just like text-containing elements (e.g. `span`).
# `a.href` always returns an absolute URL while `a.getAttribute('href')` always
# returns the exact value of the `href` attribute. For this reason we need to be sure
# that we extract the URL correctly based on the state of `absolute` option.
# that we extract the URL correctly based on the state of the `absolute` option.
href = @attr ele, 'href', @options.absolute
break unless href

# Be sure to include the title after each link reference that will be displayed at
# the end of the Markdown output, where possible.
title = @attr ele, 'title'
href += " \"#{title}\"" if title

# Try to avoid any duplicate link references.
if @linkMap[href]?
index = @linkMap[href]
title = @attr ele, 'title'
href += " \"#{title}\"" if title

# Determine what style the link should be generated in (i.e. *inline* or
# *reference*) depending on the state of the `inline` option.
suffix = if @options.inline
# *Inline* style means all links have their URLs (and possible titles) included
# immediately after their contents (e.g.
# `[my link](/path/to/page.html "My title")`).
"(#{href})"
else
index = @links.push(href) - 1
@linkMap[href] = index
# *Reference* style means all links have an index included immediately after their
# contents that directly maps to their URL (and possible title) which are displayed
# at the end of the buffer string (e.g. `[my link][0]` and then later
# `[0]: /path/to/page.html "My title"`).
# Duplicate link/title combination references are avoided for a cleaner result.
"[#{@linkMap[href] ?= @links.push(href) - 1}]"

@output '['
@atNoWS = yes
after = @outputLater "][#{index}]"
after = @outputLater "]#{suffix}"
# Images are very similar to links, just without the added complexity of references.
when 'IMG'
# Extract the image URL from `ele`.
# Unlike links, any image without a URL is just ignored. Obviously, any contents of
# an `img` element are always ignored since they can never contain anything valid.
# `img.src` always returns an absolute URL while `img.getAttribute('src')` always
# returns the exact value of the `src` attribute. For this reason we need to be sure
# that we extract the URL correctly based on the state of `absolute` option.
# that we extract the URL correctly based on the state of the `absolute` option.
skipChildren = yes
src = @attr ele, 'src', @options.absolute
break unless src
Expand Down
38 changes: 38 additions & 0 deletions test/cli_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ USAGE = """
-a, --absolute always use absolute URLs for links
-d, --debug print additional debug information
-e, --eval pass a string from the command line as input
-i, --inline generate inline style links
-l, --long-ext use long extension for Markdown files
-o, --output <dir> set the output directory for converted Markdown
-p, --print print out the converted Markdown
Expand Down Expand Up @@ -158,6 +159,43 @@ exports.absolute = do ->

""", 'Image should be absolute', '-epa')

exports.inline = do ->
testInline = (command, expected, desc, flags) ->
(test) ->
test.expect 2

exec command, (err, stdout) ->
test.ifError err, "Error should not be thrown using '#{flags}' flags"
test.equal stdout, expected, "#{desc} using #{flags} flags"

test.done()

referenceLink: testInline("#{COMMAND} -ep \"<a href='mock'>anchor</a>\"", """
[anchor][0]

[0]: mock

""", 'Link should be in reference style', '-ep')

referenceLinkWithTitle: testInline("#{COMMAND} -ep \"<a href='mock' title='mocker'>anchor</a>\"",
"""
[anchor][0]

[0]: mock "mocker"

""", 'Link should be in reference style with title', '-ep')

inlineLink: testInline("#{COMMAND} -epi \"<a href='mock'>anchor</a>\"", """
[anchor](mock)

""", 'Link should be in inline style', '-epi')

inlineLinkWithTitle: testInline("#{COMMAND} -epi \"<a href='mock' title='mocker'>anchor</a>\"",
"""
[anchor](mock "mocker")

""", 'Link should be in inline style with title', '-epi')

exports.stdio = (test) ->
test.expect 2

Expand Down
23 changes: 23 additions & 0 deletions test/core_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,29 @@ exports.options =

test.done()

inline: (test) ->
options = inline: on

test.equal md('<a href="mock">anchor</a>'), """
[anchor][0]

[0]: mock
""", 'Link should be in reference style'

test.equal md('<a href="mock" title="mocker">anchor</a>'), """
[anchor][0]

[0]: mock "mocker"
""", 'Link should be in reference style with title'

test.equal md('<a href="mock">anchor</a>', options), '[anchor](mock)',
'Link should be in inline style'

test.equal md('<a href="mock" title="mocker">anchor</a>', options), '[anchor](mock "mocker")',
'Link should be in inline style with title'

test.done()

exports.version = (test) ->
test.equal md.version, getPackage().version, 'Version should match descriptor'
test.done()