@@ -18,6 +18,8 @@ public class FreeMarker {
18
18
19
19
private Configuration cfg ;
20
20
private Map <String , Object > globalAttributes ;
21
+ private boolean isPublish ;
22
+ private File targetDir ;
21
23
22
24
public FreeMarker (File webSrcDir ) throws IOException {
23
25
cfg = new Configuration (Configuration .VERSION_2_3_24 );
@@ -46,14 +48,18 @@ public void writeFile(String template, File targetDir, String output) throws Exc
46
48
}
47
49
48
50
public void writeFile (Map <String , Object > attr , String template , File targetDir , String output ) throws Exception {
49
- Map <String , Object > attributes ;
51
+ Map <String , Object > attributes = new HashMap <>();
52
+ attributes .putAll (globalAttributes );
50
53
if (attr != null ) {
51
- attributes = new HashMap <>();
52
- attributes .putAll (globalAttributes );
53
54
attributes .putAll (attr );
54
- } else {
55
- attributes = globalAttributes ;
56
55
}
56
+ String canonical = new File (targetDir , output ).toURI ().toString ().substring (this .targetDir .toURI ().toString ().length ());
57
+ if (isPublish ) {
58
+ canonical = canonical .replaceAll ("\\ .html$" , "" );
59
+ canonical = canonical .replaceAll ("/index$" , "" );
60
+ canonical = canonical .replaceAll ("^index$" , "" );
61
+ }
62
+ attributes .put ("canonical" , attributes .get ("root" ) + canonical );
57
63
58
64
Template downloadTemplate = cfg .getTemplate (template );
59
65
@@ -88,6 +94,9 @@ public void init(Context context) {
88
94
globalAttributes .put ("archive" , false );
89
95
90
96
globalAttributes .put ("links" , context .getLinks ());
97
+
98
+ isPublish = context .config ().isPublish ();
99
+ targetDir = context .getTargetDir ();
91
100
}
92
101
93
102
}
0 commit comments