Merge pull request #209501 from sandydoo/improve/fix-commonmark-doc-isues
nixos/make-options-doc: skip re-escaping literal expressions
This commit is contained in:
commit
dce3b02d44
|
@ -21,18 +21,22 @@ parser.add_argument(
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Pretty-print certain Nix types, like literal expressions.
|
class OptionsEncoder(json.JSONEncoder):
|
||||||
def render_types(obj):
|
def encode(self, obj):
|
||||||
if '_type' not in obj: return obj
|
# Unpack literal expressions and other Nix types.
|
||||||
|
# Don't escape the strings: they were escaped when initially serialized to JSON.
|
||||||
|
if isinstance(obj, dict):
|
||||||
|
_type = obj.get('_type')
|
||||||
|
if _type is not None:
|
||||||
|
if _type == 'literalExpression' or _type == 'literalDocBook':
|
||||||
|
return obj['text']
|
||||||
|
|
||||||
_type = obj['_type']
|
if _type == 'derivation':
|
||||||
if _type == 'literalExpression' or _type == 'literalDocBook':
|
return obj['name']
|
||||||
return obj['text']
|
|
||||||
|
|
||||||
if _type == 'derivation':
|
raise Exception(f'Unexpected type `{_type}` in {json.dumps(obj)}')
|
||||||
return obj['name']
|
|
||||||
|
|
||||||
raise Exception(f'Unexpected type `{_type}` in {json.dumps(obj)}')
|
return super().encode(obj)
|
||||||
|
|
||||||
def generate_commonmark(options):
|
def generate_commonmark(options):
|
||||||
for (name, value) in options.items():
|
for (name, value) in options.items():
|
||||||
|
@ -49,14 +53,14 @@ def generate_commonmark(options):
|
||||||
if 'default' in value:
|
if 'default' in value:
|
||||||
print('*_Default_*')
|
print('*_Default_*')
|
||||||
print('```')
|
print('```')
|
||||||
print(json.dumps(value['default'], ensure_ascii=False, separators=(',', ':')))
|
print(json.dumps(value['default'], cls=OptionsEncoder, ensure_ascii=False, separators=(',', ':')))
|
||||||
print('```')
|
print('```')
|
||||||
print()
|
print()
|
||||||
print()
|
print()
|
||||||
if 'example' in value:
|
if 'example' in value:
|
||||||
print('*_Example_*')
|
print('*_Example_*')
|
||||||
print('```')
|
print('```')
|
||||||
print(json.dumps(value['example'], ensure_ascii=False, separators=(',', ':')))
|
print(json.dumps(value['example'], cls=OptionsEncoder, ensure_ascii=False, separators=(',', ':')))
|
||||||
print('```')
|
print('```')
|
||||||
print()
|
print()
|
||||||
print()
|
print()
|
||||||
|
@ -76,7 +80,7 @@ def generate_asciidoc(options):
|
||||||
print('Default::')
|
print('Default::')
|
||||||
print('+')
|
print('+')
|
||||||
print('----')
|
print('----')
|
||||||
print(json.dumps(value['default'], ensure_ascii=False, separators=(',', ':')))
|
print(json.dumps(value['default'], cls=OptionsEncoder, ensure_ascii=False, separators=(',', ':')))
|
||||||
print('----')
|
print('----')
|
||||||
print()
|
print()
|
||||||
else:
|
else:
|
||||||
|
@ -89,7 +93,7 @@ def generate_asciidoc(options):
|
||||||
print('Example::')
|
print('Example::')
|
||||||
print('+')
|
print('+')
|
||||||
print('----')
|
print('----')
|
||||||
print(json.dumps(value['example'], ensure_ascii=False, separators=(',', ':')))
|
print(json.dumps(value['example'], cls=OptionsEncoder, ensure_ascii=False, separators=(',', ':')))
|
||||||
print('----')
|
print('----')
|
||||||
print()
|
print()
|
||||||
else:
|
else:
|
||||||
|
@ -97,7 +101,7 @@ def generate_asciidoc(options):
|
||||||
print()
|
print()
|
||||||
|
|
||||||
with open(args.nix_options_path) as nix_options_json:
|
with open(args.nix_options_path) as nix_options_json:
|
||||||
options = json.load(nix_options_json, object_hook=render_types)
|
options = json.load(nix_options_json)
|
||||||
|
|
||||||
if args.format == 'commonmark':
|
if args.format == 'commonmark':
|
||||||
generate_commonmark(options)
|
generate_commonmark(options)
|
||||||
|
|
Loading…
Reference in a new issue