Skip to content

Commit

Permalink
Rename --use-server program argument to --preview
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Dec 2, 2024
1 parent 109946c commit bd7d1eb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ If you would like to familiarize yourself with Quarkdown instead, `quarkdown rep

- **`-l <dir>`** or **`--libs <dir>`**: sets the directory where external libraries can be loaded from. If unset, defaults to `<install dir>/lib/qmd`. [(?)](https://github.com/iamgio/quarkdown/wiki/importing-external-libraries)

- **`-s`** or **`--use-server`**: enables communication with the webserver, in order to enable automatic content reloading after compiling (see [*Server*](#server) below).
If a server is not running yet, it starts it and the document is opened in the default browser;
- **`-p`** or **`--preview`**: enables automatic content reloading after compiling.
If a [server](#server) is not running yet, it is started and the document is opened in the default browser;

- **`--server-port <port>`**: optional customization of the webserver's port. Defaults to `8089`.

Expand Down Expand Up @@ -233,8 +233,8 @@ The server can be started via `quarkdown start`, with the following options:

- **`-o`** or **`--open`**: if set, opens the target file in the default browser.

> [!NOTE]
> `quarkdown c ... -s` is shorthand for `quarkdown c ... && quarkdown start -f <generated file> -o`
> [!TIP]
> `quarkdown c ... --preview` is shorthand for `quarkdown c ... && quarkdown start -f <generated file> -o`
## Themes

Expand Down
11 changes: 4 additions & 7 deletions cli/src/main/kotlin/eu/iamgio/quarkdown/cli/exec/Execute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import eu.iamgio.quarkdown.log.Log
import eu.iamgio.quarkdown.pipeline.Pipeline
import eu.iamgio.quarkdown.pipeline.PipelineOptions
import eu.iamgio.quarkdown.pipeline.error.PipelineException
import eu.iamgio.quarkdown.server.browser.DefaultBrowserLauncher
import eu.iamgio.quarkdown.server.message.Reload
import eu.iamgio.quarkdown.server.message.ServerMessage
import java.io.File
Expand Down Expand Up @@ -78,22 +77,20 @@ fun runQuarkdown(
* @param startServerOnFailedConnection whether to start the server if the connection fails
*/
fun runServerCommunication(
port: Int,
targetFile: File,
startServerOnFailedConnection: Boolean,
options: WebServerOptions,
) {
// If enabled, communicates with the server to reload the requested resources, for instance in the browser.
try {
ServerMessage(Reload).send(port = port)
ServerMessage(Reload).send(port = options.port)
} catch (e: Exception) {
Log.error("Could not communicate with the server on port $port: ${e.message}")
Log.error("Could not communicate with the server on port ${options.port}: ${e.message}")
Log.debug(e)

if (startServerOnFailedConnection) {
Log.info("Starting server...")
val options = WebServerOptions(port, targetFile, DefaultBrowserLauncher())
WebServerStarter.start(options)
runServerCommunication(port, targetFile, startServerOnFailedConnection = false)
runServerCommunication(startServerOnFailedConnection = false, options)
}
}
}
23 changes: 12 additions & 11 deletions cli/src/main/kotlin/eu/iamgio/quarkdown/cli/exec/ExecuteCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import com.github.ajalt.clikt.parameters.types.int
import eu.iamgio.quarkdown.cli.CliOptions
import eu.iamgio.quarkdown.cli.exec.strategy.PipelineExecutionStrategy
import eu.iamgio.quarkdown.cli.server.DEFAULT_SERVER_PORT
import eu.iamgio.quarkdown.cli.server.WebServerOptions
import eu.iamgio.quarkdown.cli.util.thisExecutableFile
import eu.iamgio.quarkdown.media.storage.options.ReadOnlyMediaStorageOptions
import eu.iamgio.quarkdown.pipeline.PipelineOptions
import eu.iamgio.quarkdown.pipeline.error.BasePipelineErrorHandler
import eu.iamgio.quarkdown.pipeline.error.StrictPipelineErrorHandler
import eu.iamgio.quarkdown.server.browser.DefaultBrowserLauncher
import java.io.File

/**
Expand Down Expand Up @@ -102,16 +104,12 @@ abstract class ExecuteCommand(
private val noMediaStorage: Boolean by option("--no-media-storage", help = "Disables media storage").flag()

/**
* When enabled, the program communicates with the local server, for instance to dynamically reload the requested resources.
* When enabled, the program communicates with the local server to dynamically reload the requested resources.
*/
private val useServer: Boolean by option(
"-s",
"--use-server",
help = "Enable communication with the local server",
).flag()
private val preview: Boolean by option("-p", "--preview", help = "Open or reload content after compiling").flag()

/**
* Port to communicate with the local server on if [useServer] is enabled.
* Port to communicate with the local server on if [preview] is enabled.
*/
private val serverPort: Int by option("--server-port", help = "Port to communicate with the local server on").int()
.default(DEFAULT_SERVER_PORT)
Expand All @@ -132,7 +130,7 @@ abstract class ExecuteCommand(
wrapOutput = !noWrap,
workingDirectory = cliOptions.source?.parentFile,
enableMediaStorage = !noMediaStorage,
serverPort = serverPort.takeIf { useServer },
serverPort = serverPort.takeIf { preview },
mediaStorageOptionsOverrides = ReadOnlyMediaStorageOptions(),
errorHandler =
when {
Expand All @@ -148,11 +146,14 @@ abstract class ExecuteCommand(
// If enabled, communicates with the server to reload the requested resources.
// If enabled and the server is not running, also starts the server
// (this is shorthand for `quarkdown start -f <generated directory> -p <server port> -o`).
if (useServer && directory != null) {
if (preview && directory != null) {
runServerCommunication(
port = serverPort,
targetFile = directory,
startServerOnFailedConnection = true,
WebServerOptions(
port = serverPort,
targetFile = directory,
browserLauncher = DefaultBrowserLauncher(),
),
)
}
}
Expand Down

0 comments on commit bd7d1eb

Please sign in to comment.