diff --git a/README.md b/README.md index 0ca344c..725445a 100644 --- a/README.md +++ b/README.md @@ -147,8 +147,10 @@ Since this is a young project, there should be a lot of rooms for improvements. ### Buy me a coffee Maintaining this project takes time and effort, especially as I am a student now. If you find this project helpful, please consider supporting me :> -#### Paypal -[https://www.paypal.com/paypalme/brianphambinhan](https://www.paypal.com/paypalme/brianphambinhan) + + Paypal + + + Momo (Vietnam) + -#### Momo (Vietnam) -[https://me.momo.vn/brianphambinhan](https://me.momo.vn/brianphambinhan) diff --git a/README.vi.md b/README.vi.md index abe1a47..ffd6ecf 100644 --- a/README.vi.md +++ b/README.vi.md @@ -142,9 +142,10 @@ Vì đây là một dự án khá mới, hẳn sẽ có nhiều điều cần c ### Ủng hộ -#### Momo (Việt Nam) -[https://me.momo.vn/brianphambinhan](https://me.momo.vn/brianphambinhan) - -#### Paypal -[https://www.paypal.com/paypalme/brianphambinhan](https://www.paypal.com/paypalme/brianphambinhan) + + Momo (Vietnam) + + + Paypal + diff --git a/index.html b/index.html new file mode 100644 index 0000000..6199121 --- /dev/null +++ b/index.html @@ -0,0 +1,384 @@ + + + + +/media/brianhuster/D/Projects/live-preview.nvim/doc/livepreview.txt + + + + +
+
+==============================================================================
+Lua module : require("livepreview")                              livepreview
+
+Live preview for markdown, asciidoc, and html files.
+
+To work with API from this plugin, require it in your Lua code: 
+    local livepreview = require('livepreview')
+
+
+
+preview_file({filepath}, {port})                  livepreview.preview_file()
+    Start live preview
+
+    Parameters: 
+      • {filepath}  (string) path to the file
+      • {port}      (number) port to run the server on
+
+setup({opts})                                            livepreview.setup()
+    Setup live preview
+
+    Parameters: 
+      • {opts}  ({commands: {start: string, stop: string}, port: number, browser: string}, default: {start = "LivePreview", stop = "StopPreview"})
+                • commands: {start: string, stop: string} - commands to start
+                  and stop live preview
+                • port: number - port to run the server on (default: 5500)
+                • browser: string - browser to open the preview in (default:
+                  "default"). The "default" value will open the preview in
+                  system default browser.
+
+stop_preview()                                    livepreview.stop_preview()
+    Stop live preview
+
+
+==============================================================================
+Live Preview Module : SERVER                              livepreview.server
+
+Server class for live-preview.nvim To call this class, do 
+    local Server = require('livepreview').server.Server
+
+
+
+Server
+
+    Fields: 
+      • {new}        (fun(self: Server, webroot: string)) Constructor
+      • {routes}     (fun(self: Server, path: string): string) Handle routes
+      • {watch_dir}  (fun(self: Server, func: function)) Watch a directory
+                     for changes and send a message "reload" to a WebSocket
+                     client
+      • {start}      (fun(self: Server, ip: string, port: number, func: function, client: uv_tcp_t))
+                     Start the server
+      • {stop}       (fun(self: Server)) Stop the server
+
+
+Server:new({webroot})                        livepreview.server.Server:new()
+    Constructor
+
+    Parameters: 
+      • {webroot}  (string) path to the webroot
+
+Server:routes({path})                     livepreview.server.Server:routes()
+    Handle routes
+
+    Parameters: 
+      • {path}  (string) path from the http request
+
+    Return: 
+        (string) path to the file
+
+                                           livepreview.server.Server:start()
+Server:start({ip}, {port}, {func}, {client})
+    Start the server
+
+    Parameters: 
+      • {ip}      (string) IP address to bind to
+      • {port}    (number) port to bind to
+      • {func}    (function)
+      • {client}  (uv_tcp_t) uv_tcp client
+
+Server:stop()                               livepreview.server.Server:stop()
+    Stop the server
+
+Server:watch_dir({func})               livepreview.server.Server:watch_dir()
+    Watch a directory for changes and send a message "reload" to a WebSocket
+    client
+
+    Parameters: 
+      • {func}  (function) function to call when a change is detected
+
+
+==============================================================================
+Live Preview Module : HANDLER                            livepreview.handler
+
+HTTP handler module for server in live-preview.nvim To require this module, do 
+    local handler = require('livepreview.server.handler')
+
+
+
+client({client}, {callback})             livepreview.server.handler.client()
+    Handle a client connection, read the request and send a response
+
+    Parameters: 
+      • {client}    (uv_tcp_t) client connection
+      • {callback}  (function) callback function to handle the result
+                    • err: Error message, if any (nil if no error)
+                    • data: Request from the client
+
+    Return: 
+        (string) request from the client
+
+request({request})                      livepreview.server.handler.request()
+    Handle an HTTP request If the request is a websocket upgrade request, it
+    will call websocket handshake Otherwise, if it is a GET request, return
+    the path from it
+
+    Parameters: 
+      • {request}  (string) HTTP request
+
+    Return: 
+        ({path: string, if_none_match: string}?) path to the file and
+        If-None-Match header
+
+                             livepreview.server.handler.send_http_response()
+send_http_response({client}, {status}, {content_type}, {body}, {headers})
+    Send an HTTP response
+
+    Parameters: 
+      • {client}        (uv_tcp_t) client connection
+      • {status}        (string) for example "200 OK", "404 Not Found", etc.
+      • {content_type}  (string) MIME type of the response
+      • {body}          (string) body of the response
+      • {headers}       (table) (optional) additional headers to include in
+                        the response
+
+                                     livepreview.server.handler.serve_file()
+serve_file({client}, {file_path})
+    Serve a file to the client
+
+    Parameters: 
+      • {client}     (uv_tcp_t) client connection
+      • {file_path}  (string) path to the file
+
+
+==============================================================================
+Live Preview Module : WEBSOCKET                        livepreview.websocket
+
+WebSocket server implementation To require this module, do 
+    local websocket = require('livepreview.server.websocket')
+
+
+
+                                    livepreview.server.websocket.handshake()
+handshake({client}, {request})
+    Handle a WebSocket handshake request
+
+    Parameters: 
+      • {client}   (uv_tcp_t) client
+      • {request}  (string) client request
+
+send({client}, {message})                livepreview.server.websocket.send()
+    Send a message to a WebSocket client
+
+    Parameters: 
+      • {client}   (uv_tcp_t) client
+      • {message}  (string) message to send
+
+                                    livepreview.server.websocket.send_json()
+send_json({client}, {message})
+    Send a JSON message to a WebSocket client
+
+    Parameters: 
+      • {client}   (uv_tcp_t) client
+      • {message}  (table) message to send
+
+
+==============================================================================
+Live Preview Module : CONTENT_TYPE                  livepreview.content_type
+
+Content type module for the server To require this module, do 
+    local content_type = require('livepreview.server.utils.content_type')
+
+
+
+get({file_path})                 livepreview.server.utils.content_type.get()
+    Get the content type of a file
+
+    Parameters: 
+      • {file_path}  (string) path to the file
+
+    Return: 
+        (string?) content type
+
+mime                              livepreview.server.utils.content_type.mime
+    Table of file extensions and their corresponding MIME types
+
+    Example: 
+        mime["html"] -- Output : "text/html"
+
+
+
+==============================================================================
+Live Preview Module : ETAG                                  livepreview.etag
+
+ETag module To require this module, do 
+    local etag = require('livepreview.server.utils.etag')
+
+
+
+generate({file_path})               livepreview.server.utils.etag.generate()
+    Generate an ETag for a file The Etag is generated based on the
+    modification time of the file
+
+    Parameters: 
+      • {file_path}  (string) path to the file
+
+    Return: 
+        (string?) ETag
+
+
+==============================================================================
+Live Preview Module : UTILS                                livepreview.utils
+
+Utility functions for live-preview.nvim
+
+
+await_term_cmd({cmd})                     livepreview.utils.await_term_cmd()
+    Execute a shell command and wait for the exit
+
+    Parameters: 
+      • {cmd}  (string) terminal command to execute. Term_cmd will use sh or
+               pwsh depending on the OS
+
+    Return: 
+        (table) a table with fields code, stdout, stderr, signal
+
+get_parent_path                            livepreview.utils.get_parent_path
+    Get the parent path of a subpath
+
+    Example: ```lua
+    get_parent_path("/home/user/.config/nvim/lua/livepreview/utils.lua",
+    "/lua/livepreview/utils.lua") 
+        will return "/home/user/.config/nvim"
+
+
+    Parameters: 
+      • {full_path}  (string)
+      • {subpath}    (string)
+
+    Return: 
+        (string?)
+
+get_path_lua_file()                    livepreview.utils.get_path_lua_file()
+    Get path of the Lua file where the function is called
+
+    Return: 
+        (string?)
+
+get_plugin_path()                        livepreview.utils.get_plugin_path()
+    Get the path where live-preview.nvim is installed
+
+kill_port({port})                              livepreview.utils.kill_port()
+    Kill a process which is not Neovim running on a port
+
+    Parameters: 
+      • {port}  (number)
+
+open_browser({path}, {browser})             livepreview.utils.open_browser()
+    Open URL in the browser
+
+    Example: ```lua open_browser("https://neovim.io/", "firefox")
+    open_browser("https://neovim.io/", "flatpak run com.microsoft.Edge") 
+
+
+    Parameters: 
+      • {path}     (string)
+      • {browser}  (string?)
+
+sha1({val})                                         livepreview.utils.sha1()
+    Compute the SHA1 hash of a string.
+
+    Copyright (C) 2007 *Free Software Foundation, Inc*.
+
+    Parameters: 
+      • {val}  (string)
+
+    Return: 
+        (string) SHA1 hash
+
+                                      livepreview.utils.supported_filetype()
+supported_filetype({file_name})
+    Check if file name has a supported filetype (html, markdown, asciidoc).
+    Warning: this function will call a Vimscript function
+
+    Parameters: 
+      • {file_name}  (string)
+
+    Return: 
+        (filetype) string | nil
+
+term_cmd({cmd}, {callback})                     livepreview.utils.term_cmd()
+    Execute a shell commands
+
+    Parameters: 
+      • {cmd}       (string) terminal command to execute. Term_cmd will use
+                    sh or pwsh depending on the OS
+      • {callback}  (function?) function to call when the command finishes.
+                    • code: the exit code of the command
+                    • signal: the signal that killed the process
+                    • stdout: the standard output of the command
+                    • stderr: the standard error of the command
+
+uv_read_file({file_path})                   livepreview.utils.uv_read_file()
+    Read a file using libuv
+
+    Parameters: 
+      • {file_path}  (string)
+
+uv_write_file({file_path})                 livepreview.utils.uv_write_file()
+    Write a file using libuv
+
+    Parameters: 
+      • {file_path}  (string)
+
+
+==============================================================================
+Live Preview Module : HEALTH                              livepreview.health
+
+To run health check for Live Preview, run 
+    :checkhealth livepreview
+
+
+This will check if your Neovim version is compatible with Live Preview and if
+the commands to open browser are available.
+
+
+check()                                           livepreview.health.check()
+    Run health check for Live Preview. This can also be run using
+    :checkhealth livepreview
+
+    See also: 
+      • https://neovim.io/doc/user/health.html
+
+is_compatible({ver}, {range})             livepreview.health.is_compatible()
+    Check if the version is compatible with the range
+
+    Parameters: 
+      • {ver}    (string) version to check. Example: "0.10.1"
+      • {range}  (string) range to check against. Example: ">=0.10.0"
+
+    Return: 
+        (boolean) true if compatible, false otherwise
+
+
+ vim:tw=78:ts=8:sw=4:sts=4:et:ft=help:norl:
+
+
+ + diff --git a/lua/livepreview/init.lua b/lua/livepreview/init.lua index b3be806..e8f1527 100644 --- a/lua/livepreview/init.lua +++ b/lua/livepreview/init.lua @@ -52,9 +52,11 @@ function M.preview_file(filepath, port) server:start("127.0.0.1", port, function(client) if M.utils.supported_filetype(filepath) == 'html' then M.server.websocket.send_json(client, { type = "reload" }) + print("Send reload message") else local content = M.utils.uv_read_file(filepath) M.server.websocket.send_json(client, { type = "update", content = content }) + print("Send update message") end end) end) diff --git a/tests/index.html b/tests/index.html index 8b13789..ecbf79d 100644 --- a/tests/index.html +++ b/tests/index.html @@ -1 +1,556 @@ + + + + + /media/brianhuster/D/Projects/live-preview.nvim/doc/livepreview.txt + + + + + + +
+
+		==============================================================================
+		Lua module : require("livepreview") livepreview
+
+		Live preview for markdown, asciidoc, and html files.
+
+		To work with API from this plugin, require it in your Lua code: 
+			 local livepreview = require('livepreview')
+		
+
+
+		preview_file({filepath}, {port}) livepreview.preview_file()
+		Start live preview
+
+		Parameters: 
+		• {filepath} (string) path to the file
+		• {port} (number) port to run the server on
+
+		setup({opts}) livepreview.setup()
+		Setup live preview
+
+		Parameters: 
+		• {opts} ({commands: {start:
+			string, stop: string}, port: number, browser: string}, default: {start =
+		"LivePreview", stop = "StopPreview"})
+		• commands: {start: string, stop: string} - commands to start
+		and stop live preview
+		• port: number - port to run the server on (default: 5500)
+		• browser: string - browser to open the preview in (default:
+		"default"). The "default" value will open the preview in
+		system default browser.
+
+		stop_preview() livepreview.stop_preview()
+		Stop live preview
+
+
+		==============================================================================
+		Live Preview Module : SERVER livepreview.server
+
+		Server class for live-preview.nvim To call this class, do 
+			 local Server = require('livepreview').server.Server
+		
+
+
+		Server
+
+		Fields: 
+		• {new} (fun(self: Server,
+			webroot: string)) Constructor
+		• {routes} (fun(self: Server,
+			path: string): string) Handle routes
+		• {watch_dir} (fun(self:
+			Server, func: function)) Watch a directory
+		for changes and send a message "reload" to a WebSocket
+		client
+		• {start} (fun(self: Server,
+			ip: string, port: number, func: function, client: uv_tcp_t))
+		Start the server
+		• {stop} (fun(self:
+			Server)) Stop the server
+
+
+		Server:new({webroot}) livepreview.server.Server:new()
+		Constructor
+
+		Parameters: 
+		• {webroot} (string) path to the webroot
+
+		Server:routes({path}) livepreview.server.Server:routes()
+		Handle routes
+
+		Parameters: 
+		• {path} (string) path from the http request
+
+		Return: 
+		(string) path to the file
+
+		livepreview.server.Server:start()
+		Server:start({ip}, {port}, {func}, {client})
+		Start the server
+
+		Parameters: 
+		• {ip} (string) IP address to bind to
+		• {port} (number) port to bind to
+		• {func} (function)
+		• {client} (uv_tcp_t) uv_tcp client
+
+		Server:stop() livepreview.server.Server:stop()
+		Stop the server
+
+		Server:watch_dir({func}) livepreview.server.Server:watch_dir()
+		Watch a directory for changes and send a message "reload" to a WebSocket
+		client
+
+		Parameters: 
+		• {func} (function) function to call when a change is detected
+
+
+		==============================================================================
+		Live Preview Module : HANDLER livepreview.handler
+
+		HTTP handler module for server in live-preview.nvim To require this module, do 
+			 local handler = require('livepreview.server.handler')
+		
+
+
+		client({client}, {callback}) livepreview.server.handler.client()
+		Handle a client connection, read the request and send a response
+
+		Parameters: 
+		• {client} (uv_tcp_t) client connection
+		• {callback} (function) callback function to handle the result
+		• err: Error message, if
+		any (nil if no error)
+		• data: Request from the
+		client
+
+		Return: 
+		(string) request from the
+		client
+
+		request({request}) livepreview.server.handler.request()
+		Handle an HTTP request If the request is a websocket upgrade request, it
+		will call websocket handshake Otherwise, if it is a GET request, return
+		the path from it
+
+		Parameters: 
+		• {request} (string) HTTP request
+
+		Return: 
+		({path: string, if_none_match: string}?) path to the file and
+		If-None-Match header
+
+		livepreview.server.handler.send_http_response()
+		send_http_response({client}, {status}, {content_type}, {body}, {headers})
+		Send an HTTP response
+
+		Parameters: 
+		• {client} (uv_tcp_t) client connection
+		• {status} (string) for example "200 OK", "404 Not
+		Found", etc.
+		• {content_type} (string) MIME type of the response
+		• {body} (string) body of the response
+		• {headers} (table) (optional) additional headers to include in
+		the response
+
+		livepreview.server.handler.serve_file()
+		serve_file({client}, {file_path})
+		Serve a file to the client
+
+		Parameters: 
+		• {client} (uv_tcp_t) client connection
+		• {file_path} (string) path to the file
+
+
+		==============================================================================
+		Live Preview Module : WEBSOCKET livepreview.websocket
+
+		WebSocket server implementation To require this module, do 
+			 local websocket = require('livepreview.server.websocket')
+		
+
+
+		livepreview.server.websocket.handshake()
+		handshake({client}, {request})
+		Handle a WebSocket handshake request
+
+		Parameters: 
+		• {client} (uv_tcp_t) client
+		• {request} (string) client request
+
+		send({client}, {message}) livepreview.server.websocket.send()
+		Send a message to a WebSocket client
+
+		Parameters: 
+		• {client} (uv_tcp_t) client
+		• {message} (string) message to send
+
+		livepreview.server.websocket.send_json()
+		send_json({client}, {message})
+		Send a JSON message to a WebSocket client
+
+		Parameters: 
+		• {client} (uv_tcp_t) client
+		• {message} (table) message to send
+
+
+		==============================================================================
+		Live Preview Module : CONTENT_TYPE livepreview.content_type
+
+		Content type module for the server To require this module, do 
+			 local content_type = require('livepreview.server.utils.content_type')
+		
+
+
+		get({file_path}) livepreview.server.utils.content_type.get()
+		Get the content type of a file
+
+		Parameters: 
+		• {file_path} (string) path to the file
+
+		Return: 
+		(string?) content type
+
+		mime livepreview.server.utils.content_type.mime
+		Table of file extensions and their corresponding MIME types
+
+		Example: 
+			 mime["html"] -- Output : "text/html"
+		
+
+
+		==============================================================================
+		Live Preview Module : ETAG livepreview.etag
+
+		ETag module To require this module, do 
+			 local etag = require('livepreview.server.utils.etag')
+		
+
+
+		generate({file_path}) livepreview.server.utils.etag.generate()
+		Generate an ETag for a file The Etag is generated based on the
+		modification time of the file
+
+		Parameters: 
+		• {file_path} (string) path to the file
+
+		Return: 
+		(string?) ETag
+
+
+		==============================================================================
+		Live Preview Module : UTILS livepreview.utils
+
+		Utility functions for live-preview.nvim
+
+
+		await_term_cmd({cmd}) livepreview.utils.await_term_cmd()
+		Execute a shell command and wait for the exit
+
+		Parameters: 
+		• {cmd} (string) terminal command to execute. Term_cmd will use sh or
+		pwsh depending on the OS
+
+		Return: 
+		(table) a table with
+		fields code, stdout, stderr, signal
+
+		get_parent_path livepreview.utils.get_parent_path
+		Get the parent path of a subpath
+
+		Example: ```lua
+		get_parent_path("/home/user/.config/nvim/lua/livepreview/utils.lua",
+		"/lua/livepreview/utils.lua") 
+			will return "/home/user/.config/nvim"
+		
+
+		Parameters: 
+		• {full_path} (string)
+		• {subpath} (string)
+
+		Return: 
+		(string?)
+
+		get_path_lua_file() livepreview.utils.get_path_lua_file()
+		Get path of the Lua file where the function is called
+
+		Return: 
+		(string?)
+
+		get_plugin_path() livepreview.utils.get_plugin_path()
+		Get the path where live-preview.nvim is installed
+
+		kill_port({port}) livepreview.utils.kill_port()
+		Kill a process which is not Neovim running on a port
+
+		Parameters: 
+		• {port} (number)
+
+		open_browser({path}, {browser}) livepreview.utils.open_browser()
+		Open URL in the browser
+
+		Example: ```lua open_browser("https://neovim.io/", "firefox")
+		open_browser("https://neovim.io/", "flatpak run com.microsoft.Edge") 
+		
+
+		Parameters: 
+		• {path} (string)
+		• {browser} (string?)
+
+		sha1({val}) livepreview.utils.sha1()
+		Compute the SHA1 hash of a string.
+
+		Copyright (C) 2007 *Free Software Foundation, Inc*.
+
+		Parameters: 
+		• {val} (string)
+
+		Return: 
+		(string) SHA1 hash
+
+		livepreview.utils.supported_filetype()
+		supported_filetype({file_name})
+		Check if file name has a supported filetype (html, markdown, asciidoc).
+		Warning: this function will call a Vimscript function
+
+		Parameters: 
+		• {file_name} (string)
+
+		Return: 
+		(filetype) string | nil
+
+		term_cmd({cmd}, {callback}) livepreview.utils.term_cmd()
+		Execute a shell commands
+
+		Parameters: 
+		• {cmd} (string) terminal command to execute. Term_cmd will use
+		sh or pwsh depending on the OS
+		• {callback} (function?) function to call when the command finishes.
+		• code: the exit code of the command
+		• signal: the signal that killed the process
+		• stdout: the standard output of the command
+		• stderr: the standard error of the command
+
+		uv_read_file({file_path}) livepreview.utils.uv_read_file()
+		Read a file using libuv
+
+		Parameters: 
+		• {file_path} (string)
+
+		uv_write_file({file_path}) livepreview.utils.uv_write_file()
+		Write a file using libuv
+
+		Parameters: 
+		• {file_path} (string)
+
+
+		==============================================================================
+		Live Preview Module : HEALTH livepreview.health
+
+		To run health check for Live Preview, run 
+			 :checkhealth livepreview
+		
+
+		This will check if your Neovim version is compatible with Live Preview and if
+		the commands to open browser are available.
+
+
+		check() livepreview.health.check()
+		Run health check for Live Preview. This can also be run using
+		:checkhealth livepreview
+
+		See also: 
+		• https://neovim.io/doc/user/health.html
+
+		is_compatible({ver}, {range}) livepreview.health.is_compatible()
+		Check if the version is compatible with the range
+
+		Parameters: 
+		• {ver} (string) version to check. Example: "0.10.1"
+		• {range} (string) range to check against. Example: ">=0.10.0"
+
+		Return: 
+		(boolean) true if
+		compatible, false otherwise
+
+
+		vim:tw=78:ts=8:sw=4:sts=4:et:ft=help:norl:
+		
+	
+ + +