WsParser_VS/AstConv/Program.fs

111 lines
4.3 KiB
Forth

open AstAccess.AstImport
open HtmlStruct.Content
open System.Xml
open HtmlStruct.Assemble
open HtmlStruct.Present
open System.IO
open System
open System.Diagnostics
open FmtStruct.FmtEntry
let html_generate (file_in:FileInfo) (dir_o:DirectoryInfo): unit =
let out_dir = Uri(dir_o.FullName)
let doc = XmlDocument()
doc.Load(file_in.FullName)
let prog = Program.GenerateFrom(doc)
let entry = AstVisitEntry(prog)
let visitor = UnitGenerate(prog)
entry.visitWith(visitor) |> ignore
let volume_pages = volume_page_assemble(visitor.contents()) |> List.map<VolumePage, IDomUnit> (fun x->x)
let story_pages = story_page_assemble(visitor.contents()) |> List.map<StoryPage, IDomUnit> (fun x->x)
let point_pages = slice_page_assemble(volume_pages @ story_pages)
volume_pages @ story_pages @ point_pages |> List.iter (fun it -> (it:?>PageAccess).setPageRoot(out_dir))
let makers = volume_pages @ story_pages @ point_pages
|> List.map(fun page_unit ->
match page_unit with
| :? SlicePage as point ->
PageMaker(point) :> PageText
| :? VolumePage as vol ->
PageMaker(vol)
| :? StoryPage as story ->
PageMaker(story)
| _ -> failwith ""
)
let index_page = IndexPage("汇总页面", volume_pages @ story_pages @ point_pages)
index_page.setPageRoot(out_dir)
let content = (index_page :> PageText).getHtmlText()
let href = index_page.pageURL()
File.WriteAllLines(href, [content])
for refs in makers do
let file_path = (refs.bindPage() :?> PageAccess).pageURL()
File.WriteAllLines(file_path, [refs.getHtmlText()])
let graph = StorylineGraphMake("故事线网络", story_pages|> List.map(fun x -> x :?> PageAccess))
let graph_code = graph.getGraphCode()
let stream = new StreamWriter(Path.Combine(dir_o.FullName, "storys_display.dot"), false)
stream.Write(graph_code)
stream.Flush()
Process.Start("dot", $"""-Tsvg -o{Path.Combine(dir_o.FullName, "storys_display.svg")} {Path.Combine(dir_o.FullName, "storys_display.dot")}""") |> ignore
let graph2 = VolumeGraphMake("卷章引用网络", (volume_pages@story_pages)|> List.map(fun d -> d :?> PageAccess))
let graph2_code = graph2.getGraphCode()
let stream2 = new StreamWriter(Path.Combine(dir_o.FullName, "volume_display.dot"), false)
stream2.Write(graph2_code)
stream2.Flush()
Process.Start("dot", $"""-Tsvg -o{Path.Combine(dir_o.FullName, "volume_display.svg")} {Path.Combine(dir_o.FullName, "volume_display.dot")}""") |> ignore
let source_format (file_in:FileInfo): unit =
let doc = XmlDocument()
doc.Load(file_in.FullName)
let prog = Program.GenerateFrom(doc)
let entry = AstVisitEntry(prog)
let visitor = UnitGenerate(prog)
entry.visitWith(visitor) |> ignore
// 获取内容节点
let nodes = visitor.contents()
program_def_fmt (prog.dirPath()) nodes
let args = System.Environment.GetCommandLineArgs()|> Array.toList
// 参数校验
match args.Length with
| 2 when args.Item(1) = "--help" ->
printfn "AstConv.exe --file xast文件路径 --html 输出文件夹路径\n\t构建HTML透视文档。"
printfn "AstConv.exe --file xast文件路径 --format\n\t格式化所有源文件。"
exit 0
// 格式化
| 4 when args.Item(1) = "--file" && args.Item(3) = "--format" ->
if not(FileInfo(args.Item 2).Exists) then
printfn "Arguments-Error:指定xast文件不存在"
exit 0
source_format(FileInfo(args.Item 2)) |> ignore
// html生成
| 5 when args.Item(1) = "--file" && args.Item(3) = "--html" ->
if not(FileInfo(args.Item 2).Exists) then
printfn "Arguments-Error:指定xast文件不存在"
exit 0
if not(DirectoryInfo(args.Item 4).Exists) then
printfn "Arguments-Error:指定输出文件夹不存在"
exit 0
let xfile, xdir = FileInfo(args.Item 2), DirectoryInfo(args.Item 4)
// 构建html页面
html_generate xfile xdir
exit 0
| _ ->
printfn "命令调用格式错误:"
printfn "AstConv.exe --file xast文件路径 --html 输出文件夹路径\n\t构建HTML透视文档。"
printfn "AstConv.exe --file xast文件路径 --format\n\t格式化所有源文件。"
exit 0