Skip to content
Myr

scripts: add copy-examples

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This is a preliminary script to copy example code from a Markdown file into a crate's example directory. This is intended to be used for the upcoming libripgrep guide, but we don't commit any examples yet.

authored by Andrew Gallant · committed by Andrew Gallant · parent 3797a2a · Browse files · 1 file+33 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+33 added−0 removed

  • other:+33 added−0 removed

1 file+33 added−0 removed

added:scripts/copy-examples+33 added
1#!/usr/bin/env python23from __future__ import absolute_import, division, print_function4import argparse5import codecs6import os.path7import re89RE_EACH_CODE_BLOCK = re.compile(10 r'(?s)(?:```|\{\{< high rust[^>]+>\}\})[^\n]*\n(.*?)(?:```|\{\{< /high >\}\})' # noqa11)12RE_MARKER = re.compile(r'^(?:# )?//([^/].*)$')13RE_STRIP_COMMENT = re.compile(r'^# ?')1415if __name__ == '__main__':16 p = argparse.ArgumentParser()17 p.add_argument('--rust-file', default='src/cookbook.rs')18 p.add_argument('--example-dir', default='grep/examples')19 args = p.parse_args()2021 with codecs.open(args.rust_file, encoding='utf-8') as f:22 rustcode = f.read()23 for m in RE_EACH_CODE_BLOCK.finditer(rustcode):24 lines = m.group(1).splitlines()25 marker, codelines = lines[0], lines[1:]26 m = RE_MARKER.search(marker)27 if m is None:28 continue2930 code = '\n'.join(RE_STRIP_COMMENT.sub('', line) for line in codelines)31 fpath = os.path.join(args.example_dir, m.group(1))32 with codecs.open(fpath, mode='w+', encoding='utf-8') as f:33 print(code, file=f)