Gitbook文档中心搭建

一、本地安装gitbook

GitBook 是一个基于 Node.js 的命令行工具,可使用 Github/Git 和 Markdown 来制作精美的电子书。
GitBook GitHub地址:https://github.com/GitbookIO/gitbook
环境要求:

工具 版本
NodeJS v4.0.0及以上
Linux
  1. 安装命令:
$ npm install gitbook-cli -g
  1. 用命令创建一本Gitbook电子书
$ gitbook init
  1. 使用命令生成静态的web网站
$ gitbook build

或者使用命令创建gitbook serve服务,访问http://localhost:4000进行浏览。

二、项目中使用gitbook,支持word文档转化为gitbook
  1. 在linux系统中准备好gitbook环境
  2. 安装word转换为gitbook的工具,gitbook-convert;GitHub地址:https://github.com/GitbookIO/gitbook-convert
$ npm install gitbook-convert -g
  1. 找到gitbook-convert在本地的源码,
    注释gitbook-convert/lib/converters/docx.js中红框中代码:

在这里插入图片描述

  1. Maven 中jar 包依赖:
	<dependency>
		  <groupId>com.jcraft</groupId>
		  <artifactId>jsch</artifactId>
		  <version>0.1.44-1</version>
	</dependency>

  1. java 代码调用gitbook命令:
	/**
     * 导入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 <graph-path>";
      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);
    }
  }


发布了3 篇原创文章 · 获赞 0 · 访问量 66

猜你喜欢

转载自blog.csdn.net/weixin_38201175/article/details/103978356
今日推荐