Skip to content

Commit a8b7f60

Browse files
author
Anton Tayanovskyy
committed
Initial commit.
0 parents  commit a8b7f60

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin

Pandoc.Tests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.IO;
3+
using NUnit.Framework;
4+
5+
namespace Pandoc.Tests
6+
{
7+
8+
[TestFixture]
9+
public class PandocTests
10+
{
11+
private Pandoc.Processor pandoc;
12+
13+
[SetUp] public void Init()
14+
{ pandoc = new Pandoc.Processor(); }
15+
16+
[TearDown] public void Cleanup()
17+
{ pandoc.Dispose(); }
18+
19+
[Test] public void BasicConversionWorks()
20+
{
21+
var reader = new StringReader("Hello, [world](http://world.com)!");
22+
var writer = new StringWriter();
23+
pandoc.Process("markdown", "html", reader, writer);
24+
Assert.AreEqual("<p\n" +
25+
">Hello, <a href=\"http://world.com\"\n " +
26+
">world</a\n " +
27+
">!</p\n>",
28+
writer.ToString());
29+
}
30+
}
31+
}

Pandoc.build

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0"?>
2+
<!-- This is a NAnt build file. -->
3+
<project name="libpandoc" default="build" basedir=".">
4+
<target name="build">
5+
<mkdir dir="bin"/>
6+
<csc target="library" output="bin/Pandoc.dll">
7+
<sources>
8+
<include name="Pandoc.cs"/>
9+
</sources>
10+
</csc>
11+
</target>
12+
<target name="build-tests" depends="build">
13+
<csc target="library" output="bin/Pandoc.Tests.dll">
14+
<sources>
15+
<include name="Pandoc.Tests.cs"/>
16+
</sources>
17+
<references>
18+
<include name="lib/nunit.framework.dll" />
19+
<include name="bin/Pandoc.dll"/>
20+
</references>
21+
</csc>
22+
</target>
23+
<target name="test" depends="build-tests">
24+
<nunit2>
25+
<test assemblyname="bin/Pandoc.Tests.dll"/>
26+
<formatter type="Plain"/>
27+
</nunit2>
28+
</target>
29+
<target name="clean">
30+
<delete dir="bin"/>
31+
</target>
32+
</project>

Pandoc.cs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
namespace Pandoc
2+
{
3+
using System;
4+
using System.IO;
5+
using System.Text;
6+
using System.Runtime.InteropServices;
7+
8+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
9+
delegate int Reader(IntPtr data);
10+
11+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
12+
delegate void Writer(IntPtr data, int length);
13+
14+
class Native
15+
{
16+
[DllImport("libpandoc", CallingConvention=CallingConvention.Cdecl)]
17+
public static extern void pandoc_init();
18+
19+
[DllImport("libpandoc", CallingConvention=CallingConvention.Cdecl)]
20+
public static extern void pandoc_exit();
21+
22+
[DllImport("libpandoc", CallingConvention=CallingConvention.Cdecl)]
23+
public static extern string pandoc(int bufferSize,
24+
byte[] inputFormat,
25+
byte[] outputFormat,
26+
byte[] settings,
27+
Reader reader, Writer writer);
28+
}
29+
30+
public class PandocException : Exception
31+
{
32+
public PandocException(string message) : base(message)
33+
{
34+
}
35+
}
36+
37+
public class Processor : IDisposable
38+
{
39+
const int charSize = 1024;
40+
const int bytesPerChar = 4;
41+
const int byteSize = bytesPerChar * charSize;
42+
char[] chars;
43+
byte[] bytes;
44+
Encoding encoding;
45+
46+
public Processor()
47+
{
48+
chars = new char[charSize];
49+
bytes = new byte[byteSize];
50+
encoding = Encoding.UTF8;
51+
Native.pandoc_init();
52+
}
53+
54+
public void Process(string source, string target,
55+
TextReader input, TextWriter output)
56+
{
57+
Process(source, target, null, input, output);
58+
}
59+
60+
byte[] Bytes(string text)
61+
{
62+
if (text != null) {
63+
byte[] bytes = encoding.GetBytes(text);
64+
byte[] result = new byte[bytes.Length + 1];
65+
for (var i = 0; i < bytes.Length; i++) {
66+
result[i] = bytes[i];
67+
}
68+
return result;
69+
} else {
70+
return new byte[0];
71+
}
72+
}
73+
74+
public void Process(string source, string target, string config,
75+
TextReader input, TextWriter output)
76+
{
77+
Reader reader =
78+
delegate (IntPtr data)
79+
{
80+
int c = input.ReadBlock(chars, 0, charSize);
81+
int b = encoding.GetBytes(chars, 0, c, bytes, 0);
82+
Marshal.Copy(bytes, 0, data, b);
83+
return b;
84+
};
85+
Writer writer =
86+
delegate (IntPtr data, int length)
87+
{
88+
if (length > 0) {
89+
Marshal.Copy(data, bytes, 0, length);
90+
int c = encoding.GetChars(bytes, 0, length, chars, 0);
91+
output.Write(chars, 0, c);
92+
}
93+
};
94+
string err = Native.pandoc(byteSize,
95+
Bytes(source),
96+
Bytes(target),
97+
Bytes(config),
98+
reader,
99+
writer);
100+
if (err != null) {
101+
throw new PandocException(err);
102+
}
103+
}
104+
105+
public void Dispose()
106+
{
107+
Native.pandoc_exit();
108+
}
109+
}
110+
}

lib/nunit.framework.dll

61.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)