2016-05-01 26 views
1

我想擁有包含文章的單個源文件夾並生成兩個或更多個輸出變體。 (例如打印/存檔版本或特殊的移動版本的A/B測試與規範鏈接到一個變體。)Middleman博客擴展:相同源的兩個輸出變體

具有這樣

activate :blog do |blog| 
    blog.name = "variant" 
    blog.sources = "news/{year}-{month}-{day}-{title}.html" 
    blog.layout = "news/variant-layout" 
    blog.permalink = "variant/{year}/{title}.html" 
    ... 
end 
... 
activate :blog do |blog| 
    blog.name = "news" 
    blog.sources = "news/{year}-{month}-{day}-{title}.html" 
    blog.layout = "news/layout" 
    blog.permalink = "news/{year}/{title}.html" 
    ... 
end 

注一個配置當在blog.permalink的差配置來生成URL。

佈局變得混亂起來,鏈接錯了(總是指向配置文件中最後出現的版本)並且缺少頁面。

我添加了一個自定義擴展以掛鉤到中間人生命週期,將缺失的資源添加到站點地圖。 (我認爲這是一個黑客...)至少缺少的頁面出現在那之後,但是佈局錯誤和鏈接總是指向錯誤的版本。 嘗試使用不同的模板proxy似乎被阻止,因爲博客擴展本身生成動態代理頁面。 由於中間人4的asciidoc擴展尚未發佈,我目前與中間人3.4卡住了。這是一箇中間人的一般限制,我不能生成多個變體?

回答

1

我覺得你最好的行動方針是:

  • 升級到gem 'middleman', '~> 4.1.7'
  • 使用gem 'middleman-targets'

然後,您可以配置兩個構建目標:defaultvariant,就像這樣:

# config.rb 

set :target, :default 

set :targets, { 
    default: { 
    layout: 'layout-one', 
    build_dir: 'build/default', 
    target_specific_config: 'foo', 
    features : { 
     feature_one: true 
    } 
    }, 
    variant: { 
    layout: 'layout-two', 
    build_dir: 'build/variant', 
    target_specific_config: 'bar', 
    features : { 
     feature_one: false 
    } 
    } 

現在你應該可以切換完整的佈局是這樣的:

# layout.erb 

<% wrap_layout(target_value(:layout)) do %> 
    <%= yield %> 
<% end %> 

或者你可以使用的功能標誌或特定的配置值在個人網頁,就像這樣:

# page.erb 

<% if target_feature?(:feature_one) %> 
    <p>Feature One Is ON</p> 
    <p>Value is: <%= target_value(:target_specific_config) %> 
<% else %> 
    <p>Feature One Is OFF</p> 
    <p>Value is: <%= target_value(:target_specific_config) %> 
<% end %> 

文檔是有點稀少目前,所以最好在這裏閱讀源代碼的'幫助者'部分:https://github.com/middlemac/middleman-targets/blob/master/lib/middleman-targets/extension.rb

相關問題