Skip to content

Commit

Permalink
Add tests and documentation for useRelative
Browse files Browse the repository at this point in the history
  • Loading branch information
RobLoach committed Feb 14, 2014
1 parent 3d36077 commit 5f254b3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 2 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,42 @@ Then we will stick a few images inside our path: `src/files/associated-files/my-
</p>
```

Ins't that cool?

## Configure

### Defaults

The default configuration for this plugin is the equivalant of adding the following options to your [DocPad configuration file](http://docpad.org/docs/config):

``` coffee
plugins:
associatedfiles:
# The paths for the associated files.
associatedFilesPath: 'associated-files'

# Whether to use relative base paths for the document. This would
# use associated-files/subfolder/myarticle/image.jpg instead of
# associated-files/myarticle/image.jpg.
useRelativeBase: false
```

### Template Configuration

It is possible to override the default configuration on a per-template basis:

``` html
---
associatedFilesRelative: true
associatedFilesPath: './myfolder'
---

<% for file in @getDocument().getAssociatedFiles().toJSON(): %>
<p>
<h3><%= file.title or file.name %></h3>
<img src="<%= file.url %>" title="<%= file.title or file.name %>" />
</p>
<% end %>
```


<!-- HISTORY/ -->
Expand Down
7 changes: 6 additions & 1 deletion src/associatedfiles.plugin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ module.exports = (BasePlugin) ->

# Extend our prototype
DocumentModel::getAssociatedFilesPath = ->
documentBase = if config.useRelativeBase then @get('relativeBase') else @get('basename')
# Retrieve the document base name, minding relative paths.
documentBase = if config.useRelativeBase or @get('associatedFilesRelative') then @get('relativeBase') else @get('basename')

# Find the associated files path for the document.
documentAssociatedFilesPath = @get('associatedFilesPath') or documentBase

# Get the clean path for the associated files.
documentAssociatedFilesPathNormalized = @getPath(documentAssociatedFilesPath, associatedFilesPath)
unless documentAssociatedFilesPathNormalized.slice(-1) in ['\\','/']
documentAssociatedFilesPathNormalized += pathUtil.sep
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c
1 change: 1 addition & 0 deletions test/out-expected/subfolder/test3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["test3-a.txt"]
5 changes: 5 additions & 0 deletions test/src/documents/subfolder/test3.html.eco
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
associatedFilesRelative: true
---

<%- JSON.stringify @getDocument().getAssociatedFiles().pluck('filename') %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c

0 comments on commit 5f254b3

Please sign in to comment.