Skip to content

Commit

Permalink
Merge pull request #16 from igapyon/feature_20170117a
Browse files Browse the repository at this point in the history
Feature 20170117a
  • Loading branch information
igapyon authored Jan 21, 2017
2 parents 8153ada + 424420e commit ecd6a84
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>jp.igapyon.diary.v3</groupId>
<artifactId>IgapyonV3</artifactId>
<version>1.2.4</version>
<version>1.2.5</version>
<packaging>jar</packaging>

<name>IgapyonV3</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import jp.igapyon.diary.v3.mdconv.freemarker.directive.LinkSearchDirectiveModel;
import jp.igapyon.diary.v3.mdconv.freemarker.directive.LinkShareDirectiveModel;
import jp.igapyon.diary.v3.mdconv.freemarker.directive.LocalRssDirectiveModel;
import jp.igapyon.diary.v3.mdconv.freemarker.directive.LocalYearlistDirectiveModel;
import jp.igapyon.diary.v3.mdconv.freemarker.directive.RSSFeedDirectiveModel;
import jp.igapyon.diary.v3.util.IgapyonV3Settings;
import jp.igapyon.diary.v3.util.SimpleDirUtil;
Expand Down Expand Up @@ -134,6 +135,7 @@ public static Configuration getConfiguration(final IgapyonV3Settings settings, f
config.setSharedVariable("linkshare", new LinkShareDirectiveModel(settings));
config.setSharedVariable("linkmap", new LinkMapDirectiveModel(settings));
config.setSharedVariable("linkamazon", new LinkAmazonDirectiveModel(settings));
config.setSharedVariable("localyearlist", new LocalYearlistDirectiveModel(settings));

return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,31 +192,6 @@ public static String getFirstH2String(final File file) throws IOException {
return firstH2Line;
}

public static String getYearListMdString() {
return "[2017](https://igapyon.github.io/diary/2017/index.html)\n"
+ "/ [2016](https://igapyon.github.io/diary/2016/index.html)\n"
+ "/ [2015](https://igapyon.github.io/diary/2015/index.html)\n"
+ "/ [2014](https://igapyon.github.io/diary/2014/index.html)\n"
+ "/ [2013](https://igapyon.github.io/diary/2013/index.html)\n"
+ "/ [2012](https://igapyon.github.io/diary/2012/index.html)\n"
+ "/ [2011](https://igapyon.github.io/diary/2011/index.html)\n"
+ "/ [2010](https://igapyon.github.io/diary/2010/index.html)\n"
+ "/ [2009](https://igapyon.github.io/diary/2009/index.html)\n"
+ "/ [2008](https://igapyon.github.io/diary/2008/index.html)\n"
+ "/ [2007](https://igapyon.github.io/diary/2007/index.html)\n"
+ "/ [2006](https://igapyon.github.io/diary/2006/index.html)\n"
+ "/ [2005](https://igapyon.github.io/diary/2005/index.html)\n"
+ "/ [2004](https://igapyon.github.io/diary/2004/index.html)\n"
+ "/ [2003](https://igapyon.github.io/diary/2003/index.html)\n"
+ "/ [2002](https://igapyon.github.io/diary/2002/index.html)\n"
+ "/ [2001](https://igapyon.github.io/diary/2001/index.html)\n"
+ "/ [2000](https://igapyon.github.io/diary/2000/index.html)\n"
+ "/ [1998](https://igapyon.github.io/diary/1998/index.html)\n"
+ "/ [1997](https://igapyon.github.io/diary/1997/index.html)\n"
+ "/ [1996](https://igapyon.github.io/diary/1996/index.html)\n"
+ "/ [ALL](https://igapyon.github.io/diary/idxall.html)\n";
}

@Override
public Object findTemplateSource(final String resourceName) throws IOException {
if ("diaryYearList".equals(resourceName)) {
Expand All @@ -225,7 +200,7 @@ public Object findTemplateSource(final String resourceName) throws IOException {

ensureLoadAtomXml();

final File actualFile = new File(stripLocaleName(resourceName));
final File actualFile = new File(settings.getRootdir(), stripLocaleName(resourceName));
final String body = FileUtils.readFileToString(actualFile, "UTF-8");
String load = body;

Expand Down Expand Up @@ -409,7 +384,7 @@ public Object findTemplateSource(final String resourceName) throws IOException {
@Override
public Reader getReader(final Object templateSource, final String encoding) throws IOException {
if ("diaryYearList".equals((String) templateSource)) {
return new StringReader(getYearListMdString());
throw new IOException("ERROR: diaryYearList is removed. use directive instead.");
}

return new StringReader(resourceMap.get(templateSource));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Igapyon Diary system v3 (IgapyonV3).
* Copyright (C) 2015-2017 Toshiki Iga
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright 2015-2017 Toshiki Iga
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package jp.igapyon.diary.v3.mdconv.freemarker.directive;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import jp.igapyon.diary.v3.util.IgapyonV3Settings;
import jp.igapyon.diary.v3.util.SimpleDirParser;

/**
* ローカルの年リスト用のディレクティブモデル
*
* <@localyearlist />
*
* @author Toshiki Iga
*/
public class LocalYearlistDirectiveModel implements TemplateDirectiveModel {
private IgapyonV3Settings settings = null;

public LocalYearlistDirectiveModel(final IgapyonV3Settings settings) {
this.settings = settings;
}

public void execute(final Environment env, @SuppressWarnings("rawtypes") final Map params,
final TemplateModel[] loopVars, final TemplateDirectiveBody body) throws TemplateException, IOException {
final BufferedWriter writer = new BufferedWriter(env.getOut());

final List<File> files = getLocalYearList(settings.getRootdir());

boolean isFirst = true;
for (int index = files.size() - 1; index >= 0; index--) {
if (isFirst) {
isFirst = false;
} else {
writer.write("/ ");
}

final File file = files.get(index);
writer.write("[" + file.getName() + "](" + settings.getBaseurl() + "/" + file.getName() + "/index.html)\n");
}

writer.write("/ [ALL](" + settings.getBaseurl() + "/idxall.html)\n");

writer.flush();
}

public static List<File> getLocalYearList(final File rootdir) {
final SimpleDirParser parser = new SimpleDirParser() {
final Pattern pat = Pattern.compile("^[0-9][0-9][0-9][0-9]$");

@Override
public boolean isProcessTarget(final File file) {
if (file.isDirectory() == false) {
return false;
}
final Matcher mat = pat.matcher(file.getName());
if (mat.find()) {
// 年の形式のみ対象。
return true;
}

return false;
}
};

return parser.listFiles(rootdir, false);
}
}
17 changes: 17 additions & 0 deletions src/test/java/jp/igapyon/diary/v3/directive/DirectiveTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package jp.igapyon.diary.v3.directive;

import java.io.File;

import org.junit.Test;

import jp.igapyon.diary.v3.mdconv.DiarySrcMd2MdConverter;
import jp.igapyon.diary.v3.util.IgapyonV3Settings;

public class DirectiveTest {
@Test
public void test() throws Exception {
final IgapyonV3Settings settings = new IgapyonV3Settings();
settings.setRootdir(new File("./test/data"));
new DiarySrcMd2MdConverter(settings).processDir(new File("./test/data/directive/localyearlist"));
}
}
16 changes: 16 additions & 0 deletions test/data/directive/localyearlist/test_localyearlist.html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[top](https://igapyon.github.io/diary/)

Test
=====================================================================================================
[![いがぴょん画像(小)](https://igapyon.github.io/diary/images/iga200306s.jpg "いがぴょん")](https://igapyon.github.io/diary/memo/memoigapyon.html) 日記形式でつづる [いがぴょん](https://igapyon.github.io/diary/memo/memoigapyon.html)コラム ウェブページです。

## Test

[2017](https://igapyon.github.io/diary/2017/index.html)
/ [ALL](https://igapyon.github.io/diary/idxall.html)


----------------------------------------------------------------------------------------------------

## この日記について
[いがぴょんについて](https://igapyon.github.io/diary/memo/memoigapyon.html) / [日記ジェネレータ](https://github.com/igapyon/igapyonv3)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Test

<@localyearlist />

0 comments on commit ecd6a84

Please sign in to comment.