GitBook 是一个基于 Node.js 的命令行工具,可使用 Github/Git 和 Markdown 来制作精美的电子书。
GitBook GitHub地址:https://github.com/GitbookIO/gitbook
环境要求:
工具 | 版本 |
---|---|
NodeJS | v4.0.0及以上 |
Linux |
$ npm install gitbook-cli -g
$ gitbook init
$ gitbook build
或者使用命令创建gitbook serve
服务,访问http://localhost:4000进行浏览。
$ npm install gitbook-convert -g
com.jcraft
jsch
0.1.44-1
/**
* 导入word文档,用命令将word文档转为gitbook
*/
public Map<String,Object> Doc2Gitbook(InDTO in){
Map<String,Object> map = JSON.parseObject(in.getBody(), Map.class);
LOGGER.info("转word成gitbook");
String filePath = CommonUtil.toString(map.get("hiddenFilePath"));
String fileDir = filePath.substring(0, 8);
String fileName = filePath.substring(8);
String staticFileName = filePath.substring(0,22);
Map<String,Object> resultMap = new HashMap<String, Object>();
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");//设置日期格式
String today=df.format(new Date());//格式化当前日期
String command = "cd /*/"+today+";"
+ "export NODE_HOME=/*/node-v*0-linux-x64;"
+ "export PATH=$NODE_HOME/bin:$PATH;"
+ "gitbook-convert "+fileName+" /*/tomcat-*/webapps/ROOT/gitbookDocx/"+staticFileName
+";cd /*/tomcat-*/webapps/ROOT/gitbookDocx/"+staticFileName
+ ";gitbook build";
int result = 0;
int cmdResult = 0;
try {
result = SftpUtil.execCommand(GITBOOK_HOST, GITBOOK_PORT, GITBOOK_USERNAME,GITBOOK_PWD,command);
} catch (JSchException e) {
e.printStackTrace();
resultMap.put("status", "2");
}
if((result==0 || result==1) && (cmdResult==0 || cmdResult==1)){
resultMap.put("status", "1");
resultMap.put("pathPart", staticFileName);
}
return resultMap;
}
/**
* 执行shell脚本
* @param host 主机名
* @param port 端口
* @param username 用户名
* @param password 密码
* @param command:shell脚本
*/
public static int execCommand(final String host, final int port, final String username,
final String password,String command) throws JSchException {
int returnCode = 0;
Vector<String> stdout = new Vector<String>();
JSch jsch = new JSch();
MyUserInfo userInfo = new MyUserInfo();
try {
Session session = jsch.getSession(username, host ,port);
session.setPassword(password);
session.setUserInfo(userInfo);
session.connect();
// Create and connect channel.
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// ChannelShell channel = (ChannelShell) session.openChannel("shell");
channel.setInputStream(null);
BufferedReader input = new BufferedReader(new InputStreamReader(channel.getInputStream()));
channel.connect();
//==================start输出命令执行结果===========================
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
byte[] tmp=new byte[1024];
String page_message = "";
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
page_message=new String(tmp, 0, i);
System.out.print(page_message);
}
if(channel.isClosed()){
if(in.available()>0) continue;
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
//==================end输出命令执行结果==================================
System.out.println("The remote command is: " + command);
// Get the output of remote command.
String line;
while ((line = input.readLine()) != null) {
stdout.add(line);
}
input.close();
if (channel.isClosed()) {
returnCode = channel.getExitStatus();
}
channel.disconnect();//关闭渠道
session.disconnect();//关闭session
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return returnCode;
}
public static void connect(){
try{
JSch jsch=new JSch();
String host="*****";
String user="*****";
String config =
"Host foo\n"+
" User "+user+"\n"+
" Hostname "+host+"\n";
ConfigRepository configRepository =
com.jcraft.jsch.OpenSSHConfig.parse(config);
jsch.setConfigRepository(configRepository);
Session session=jsch.getSession("foo");
String passwd ="*****";
session.setPassword(passwd);
UserInfo ui = new MyUserInfo(){
public boolean promptYesNo(String message){
int foo = 0;
return foo==0;
}
};
session.setUserInfo(ui);
session.connect();
String command="air sandbox run " ;
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
page_message=new String(tmp, 0, i);
System.out.print(page_message);
}
if(channel.isClosed()){
if(in.available()>0) continue;
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
}
catch(Exception e){
System.out.println(e);
}
}