]> git.codecow.com Git - Monocypher.git/commitdiff
Add a script to extract examples from man pages
authorMichael Savage <mikejsavage@gmail.com>
Fri, 27 Dec 2019 16:47:47 +0000 (18:47 +0200)
committerMichael Savage <mikejsavage@gmail.com>
Fri, 27 Dec 2019 16:47:47 +0000 (18:47 +0200)
doc/man/man3/test_examples.lua [new file with mode: 0755]

diff --git a/doc/man/man3/test_examples.lua b/doc/man/man3/test_examples.lua
new file mode 100755 (executable)
index 0000000..37a1c49
--- /dev/null
@@ -0,0 +1,42 @@
+#! /usr/bin/env lua
+
+local lfs = require( "lfs" )
+
+local all = {
+       "#include \"../../../src/monocypher.h\"",
+       "#include \"../../../src/optional/monocypher-ed25519.h\"",
+       "#include <stdlib.h>",
+       "int main() {",
+}
+
+local function AddExamples( man )
+       for code in man:gmatch( "%.Bd[^\n]*\n(.-)%.Ed" ) do
+               table.insert( all, "{" )
+               table.insert( all, code )
+               table.insert( all, "}" )
+       end
+end
+
+local function AddDir( path )
+       for file in lfs.dir( path ) do
+               local attr = lfs.symlinkattributes( path .. "/" .. file )
+               if file:match( "%.3monocypher$" ) and attr.mode == "file" then
+                       table.insert( all, "// " .. path .. "/" .. file )
+
+                       local f = assert( io.open( path .. "/" .. file, "r" ) )
+                       local contents = assert( f:read( "*all" ) )
+                       f:close()
+
+                       AddExamples( contents )
+
+                       table.insert( all, "" )
+               end
+       end
+end
+
+AddDir( "." )
+AddDir( "optional" )
+
+table.insert( all, "}" )
+
+print( table.concat( all, "\n" ) )