Class: Dyndoc::InteractiveServer

Inherits:
Object
  • Object
show all
Defined in:
lib/dyndoc/srv/interactive-server.rb

Instance Method Summary collapse

Constructor Details

#initializeInteractiveServer

Returns a new instance of InteractiveServer.



10
11
12
13
14
15
# File 'lib/dyndoc/srv/interactive-server.rb', line 10

def initialize
  @tmpl_mngr=nil
  @tmpl_filename=nil
  init_dyndoc
  init_server
end

Instance Method Details

#init_dyndocObject



17
18
19
20
21
22
23
24
# File 'lib/dyndoc/srv/interactive-server.rb', line 17

def init_dyndoc
  unless @tmpl_mngr
    Dyndoc.cfg_dyn['dyndoc_session']=:interactive
    @tmpl_mngr = Dyndoc::Ruby::TemplateManager.new({})
    ##is it really well-suited for interactive mode???
  end
  reinit_dyndoc
end

#init_serverObject



69
70
71
# File 'lib/dyndoc/srv/interactive-server.rb', line 69

def init_server
  @server = TCPServer.new('0.0.0.0',7777)
end

#post_process_from_format(content, fmt) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dyndoc/srv/interactive-server.rb', line 35

def post_process_from_format(content,fmt)
  case fmt
  when :rmd
    ::Dyndoc::Converter.tag!(:md)
    ::Dyndoc::Converter.markdown(content)
  when :adoc
    ::Dyndoc::Converter.tag!(:adoc)
    ::Dyndoc::Converter.asciidoctor(content)
  when :ttm
    ::Dyndoc::Converter.tag!(:ttm)
    ::Dyndoc::Converter.ttm(content,"-e2 -r -y1 -L").gsub(/<mtable[^>]*>/,"<mtable>").gsub("\\ngtr","<mtext>&ngtr;</mtext>").gsub("\\nless","<mtext>&nless;</mtext>").gsub("&#232;","<mtext>&egrave;</mtext>")
  end
end

#process_dyndoc(content) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dyndoc/srv/interactive-server.rb', line 50

def process_dyndoc(content)
  ##Dyndoc.warn :process_dyndoc_content,content
  @content=@tmpl_mngr.parse(content)
  ##Dyndoc.warn :content, @content
  ## depending on extension
  if @tmpl_filename =~ /_(rmd|adoc|ttm)\.dyn$/
    @content=post_process_from_format(@content,$1.to_sym)
  end
  @tmpl_mngr.filterGlobal.envir["body.content"]=Dyndoc::Utils.protect_dyn_block_for_atom(@content)
  ##Dyndoc.warn :body_content,@tmpl_mngr.filterGlobal.envir["body.content"]
  if @tmpl_filename
    @tmpl_mngr.filterGlobal.envir["_FILENAME_CURRENT_"]=@tmpl_filename.dup
    @tmpl_mngr.filterGlobal.envir["_FILENAME_"]=@tmpl_filename.dup #register name of template!!!
    @tmpl_mngr.filterGlobal.envir["_FILENAME_ORIG_"]=@tmpl_filename.dup #register name of template!!!
    @tmpl_mngr.filterGlobal.envir["_PWD_"]=File.dirname(@tmpl_filename)
  end
  return Dyndoc::Utils.unprotect_dyn_block_for_atom(@content)
end

#reinit_dyndocObject



26
27
28
29
30
31
32
33
# File 'lib/dyndoc/srv/interactive-server.rb', line 26

def reinit_dyndoc
  if @tmpl_mngr
    @tmpl_mngr.init_doc({:format_output=> "html"})
    @tmpl_mngr.require_dyndoc_libs("DyndocWebTools")
    puts "InteractiveServer (re)initialized!\n"
    @tmpl_mngr.as_default_tmpl_mngr! #=> Dyndoc.tmpl_mngr activated!
  end
end

#runObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/dyndoc/srv/interactive-server.rb', line 73

def run
	trap("SIGINT") { @server.close;exit! }
loop {
	socket = @server.accept

    ## Rmk: I can't remember the whole story but I started with recv and then need read (but did not notice that the atom-plugin did not work anymore). gets() seems to be the solution.
    ## read and gets are blocking and recv is not
    b=socket.gets("__[[END_TOKEN]]__")
	#b=socket.read
    #b=socket.recv(100000)

	##p [:b,b]
	data=b.to_s.strip
	##p [:data,data]
	if data =~ /^__send_cmd__\[\[([a-z,_]*)\|?([^\]]*)?\]\]__(.*)__\[\[END_TOKEN\]\]__$/m
      cmd,@tmpl_filename,content = $1,$2,$3
			##p [:cmd,cmd,:content,content,:filename,@tmpl_filename]
			#p [:tmpl_mngr,@tmpl_filename]
      unless @tmpl_filename.empty?
        Question.session_dir(File.dirname(@tmpl_filename))
      end
      if content.strip == "__EXIT__"
				socket.close
				@server.close
				break
			end

      if cmd =~ /(.*)_with_layout_reinit$/
          LayoutMngr.reinit
          cmd=$1
      end

      if cmd =~ /(.*)_with_libs_reinit$/
          reinit_dyndoc
          cmd=$1
      end

			if cmd == "dyndoc"
			  res = process_dyndoc(content)
			  ##p [:dyndoc_server,content,res]
			  socket.write "__send_cmd__[[dyndoc]]__"+res+"__[[END_TOKEN]]__"
      end
	end
	socket.close
}
end