Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preprocessor in Python? #975

Closed
flodolo opened this issue Jul 8, 2019 · 6 comments · Fixed by #1480
Closed

Preprocessor in Python? #975

flodolo opened this issue Jul 8, 2019 · 6 comments · Fixed by #1480
Labels
A-API Area: API A-Documentation Area: Documentation

Comments

@flodolo
Copy link

flodolo commented Jul 8, 2019

I'm trying to figure out how to write a preprocessor in Python, given that it's listed in the example, but I can't even get started.

  1. A simple script that read stdin and write to stdout without any changes
import sys

sys.stdout.writelines(sys.stdin)
sys.stdout.flush()

Results in

2019-07-08 10:35:25 [ERROR] (mdbook::utils): Error: Unable to parse the preprocessed book
2019-07-08 10:35:25 [ERROR] (mdbook::utils): 	Caused By: invalid type: map, expected a sequence at line 1 column 1

Clearly I'm missing something obvious.

  1. Path to the script. I can call a script living in the same path as book.toml, but relative paths don't seem to work?
@ehuss
Copy link
Contributor

ehuss commented Jul 9, 2019

Preprocessors use a particular JSON api to exchange input and output. I believe the input is an array of [PreprocessorContext, Book] and the output is just a serialized Book. This isn't really documented, and I'm not sure if that is intentional.

@ehuss ehuss added A-API Area: API A-Documentation Area: Documentation labels Jul 9, 2019
@flodolo
Copy link
Author

flodolo commented Jul 9, 2019

import json
import sys

# Read book data from stdin
book_data = json.load(sys.stdin)

This fails with mdbook build(json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)). But, if I write to file the content of stdin once, then pipe it into this script, the JSON is correctly interpreted. And I have no clue why it would happen.

@ehuss
Copy link
Contributor

ehuss commented Jul 9, 2019

A minimal preprocessor in Python would look like this:

#!/usr/bin/env python

import json
import sys

if len(sys.argv) > 1:
    if sys.argv[1] == 'supports':
        # sys.argv[2] is the renderer name
        sys.exit(0)

context, book = json.load(sys.stdin)
json.dump(book, sys.stdout)

@henryiii
Copy link

This was exactly what I was looking for. The docs does not really make it clear what the output should be. I've implemented a preprocessor in Python that sets up a tabbed interface for code or other content here.

@winternet-studio
Copy link

I was also confused by this - first that I only had to return the second array entry (which I now do see that the documentation states!) - but more importantly this thing about calling the script twice, which isn't specifically mentioned (only if you sit and think long and hard about the example code).

Here is my renderer in PHP I finally got working:

<?php
if (@$argv[1] == 'supports') {
	// name of renderer is in $argv[2]
	exit(0);  //currently support all renderers
}

$content = '';
while (false !== ($line = fgets(STDIN))) {
	$content .= $line;
}

$data = json_decode($content);
$secondEntry = $data[1];

// Do you transformations to $secondEntry here...

echo json_encode($secondEntry);

@LunarLambda
Copy link

LunarLambda commented Sep 27, 2023

import json
import sys

# Read book data from stdin
book_data = json.load(sys.stdin)

This fails with mdbook build(json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)). But, if I write to file the content of stdin once, then pipe it into this script, the JSON is correctly interpreted. And I have no clue why it would happen.

I'm also getting this error with the code outlined in the documentation.

book.toml has

[preprocessor.xnos]
command = "python3 ./xnos.py --"

Removing the -- fixed it. Eh?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-API Area: API A-Documentation Area: Documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants