1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.rwx.maven.asciidoc.services.impl;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import net.rwx.maven.asciidoc.backends.Backend;
23 import net.rwx.maven.asciidoc.configuration.Document;
24 import net.rwx.maven.asciidoc.services.AsciidocService;
25 import net.rwx.maven.asciidoc.utils.FileUtils;
26 import org.python.core.PyString;
27 import org.python.core.PySystemState;
28 import org.python.util.PythonInterpreter;
29
30 public class AsciidocServiceImpl extends RootServiceImpl implements AsciidocService {
31
32 private String asciidocProgramPath;
33
34 public AsciidocServiceImpl() throws IOException {
35 ClassLoader loader = this.getClass().getClassLoader();
36 InputStream is = loader.getResourceAsStream( ARCHIVE_NAME );
37 String temporaryDirectory = FileUtils.getTemporaryDirectory();
38 FileUtils.uncompress( is, temporaryDirectory );
39 setAsciidocProgramPath( temporaryDirectory );
40 }
41
42
43
44
45
46
47
48
49 @Override
50 public void execute( String inputPath, Document document, Backend backend ) throws Exception {
51 setOuputPath(inputPath, backend);
52 PySystemState state = getPySystemState( inputPath, backend.getName(), getOuputPath() );
53
54 PythonInterpreter interp = new PythonInterpreter( null, state );
55 interp.set( "__file__", asciidocProgramPath );
56 interp.execfile( asciidocProgramPath );
57 }
58
59
60
61
62
63
64
65
66
67 private PySystemState getPySystemState( String input, String backend, String output ) {
68 PySystemState state = new PySystemState();
69
70 state.argv.clear();
71 state.argv.append( new PyString( asciidocProgramPath ) );
72 state.argv.append( new PyString( "-b" ) );
73 state.argv.append( new PyString( backend ) );
74 state.argv.append( new PyString( "-a" ) );
75 state.argv.append( new PyString( "icons" ) );
76 state.argv.append( new PyString( "--out-file=" + output ) );
77 state.argv.append( new PyString( input ) );
78
79 File fInput = new File( input );
80 state.setCurrentWorkingDir( fInput.getParent() );
81
82 return state;
83 }
84
85
86
87
88 private void setAsciidocProgramPath( String temporaryDirectory ) {
89 StringBuilder builder = new StringBuilder();
90 builder.append( temporaryDirectory );
91 builder.append( File.separator );
92 builder.append( "asciidoc-" );
93 builder.append( VERSION );
94 builder.append( File.separator );
95 builder.append( "asciidoc.py" );
96
97 asciidocProgramPath = builder.toString();
98 }
99
100 @Override
101 protected void setOuputPath(String inputPath, Backend backend) {
102 setOutputPath(inputPath, backend.getAsciidocExtension());
103 }
104 }