Skip to content

Commit

Permalink
Use quarkdown c -s as shorthand for quarkdown c && quarkdown start
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Dec 2, 2024
1 parent 9a9b711 commit 109946c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +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`**: inject additional code to communicate to the webserver, in order to reload the browser automatically after compiling;
- **`-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;

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

Expand Down Expand Up @@ -232,6 +233,9 @@ 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`
## Themes

Quarkdown comes with a set of themes that can give a unique look to your document.
Expand Down
40 changes: 32 additions & 8 deletions cli/src/main/kotlin/eu/iamgio/quarkdown/cli/exec/Execute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import eu.iamgio.quarkdown.cli.CliOptions
import eu.iamgio.quarkdown.cli.PipelineInitialization
import eu.iamgio.quarkdown.cli.exec.strategy.PipelineExecutionStrategy
import eu.iamgio.quarkdown.cli.lib.QmdLibraries
import eu.iamgio.quarkdown.cli.server.WebServerOptions
import eu.iamgio.quarkdown.cli.server.WebServerStarter
import eu.iamgio.quarkdown.cli.util.cleanDirectory
import eu.iamgio.quarkdown.cli.util.saveTo
import eu.iamgio.quarkdown.flavor.MarkdownFlavor
Expand All @@ -13,21 +15,24 @@ 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
import kotlin.system.exitProcess

/**
* Executes a complete Quarkdown pipeline.
* @param executionStrategy launch strategy of the pipeline, e.g. from file or REPL
* @param cliOptions options that define the behavior of the CLI, especially I/O
* @param pipelineOptions options that define the behavior of the pipeline
* @return the output file or directory, if any
*/
fun runQuarkdown(
executionStrategy: PipelineExecutionStrategy,
cliOptions: CliOptions,
pipelineOptions: PipelineOptions,
) {
): File? {
// Flavor to use across the pipeline.
val flavor: MarkdownFlavor = QuarkdownFlavor

Expand Down Expand Up @@ -57,19 +62,38 @@ fun runQuarkdown(
val resource = executionStrategy.execute(pipeline)

// Exports the generated resources to file if enabled in options.
directory?.let { resource?.saveTo(it) }
return directory?.let { resource?.saveTo(it) }
} catch (e: PipelineException) {
e.printStackTrace()
exitProcess(e.code)
}
}

/**
* Communicates with the server to reload the requested resources.
* If the server is not running, starts it if [startServerOnFailedConnection] is `true`
* and tries to communicate again.
* @param port port to communicate with the server on
* @param targetFile file to serve
* @param startServerOnFailedConnection whether to start the server if the connection fails
*/
fun runServerCommunication(
port: Int,
targetFile: File,
startServerOnFailedConnection: Boolean,
) {
// If enabled, communicates with the server to reload the requested resources, for instance in the browser.
pipelineOptions.serverPort?.let {
try {
ServerMessage(Reload).send(port = it)
} catch (e: Exception) {
Log.error("Could not communicate with the server on port $it: ${e.message}")
Log.debug(e)
try {
ServerMessage(Reload).send(port = port)
} catch (e: Exception) {
Log.error("Could not communicate with the server on port $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)
}
}
}
20 changes: 18 additions & 2 deletions cli/src/main/kotlin/eu/iamgio/quarkdown/cli/exec/ExecuteCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ abstract class ExecuteCommand(
/**
* When enabled, the program communicates with the local server, for instance 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 useServer: Boolean by option(
"-s",
"--use-server",
help = "Enable communication with the local server",
).flag()

/**
* Port to communicate with the local server on if [useServer] is enabled.
Expand Down Expand Up @@ -138,6 +142,18 @@ abstract class ExecuteCommand(
)

// Executes the Quarkdown pipeline.
runQuarkdown(createExecutionStrategy(cliOptions), cliOptions, pipelineOptions)
// If generated, the output directory is returned, which is a child of pipelineOptions.outputDirectory.
val directory: File? = runQuarkdown(createExecutionStrategy(cliOptions), cliOptions, pipelineOptions)

// 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) {
runServerCommunication(
port = serverPort,
targetFile = directory,
startServerOnFailedConnection = true,
)
}
}
}
5 changes: 3 additions & 2 deletions cli/src/main/kotlin/eu/iamgio/quarkdown/cli/util/IOUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ fun File.cleanDirectory() {
/**
* Saves [this] resource to file in a [directory].
* @see FileResourceExporter
* @return the saved file
*/
fun OutputResource.saveTo(directory: File) {
accept(FileResourceExporter(location = directory))
fun OutputResource.saveTo(directory: File): File {
return accept(FileResourceExporter(location = directory))
}

0 comments on commit 109946c

Please sign in to comment.