ProductivityTools.ConvertDocuments.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

function ExtractPanDoc{
    [CmdletBinding()]
    param()

    $path="$PSScriptRoot\Pandoc.zip"
    Write-Verbose "Trying to extract zip $path"

    Expand-Archive -Path $path -DestinationPath $PSScriptRoot
    
}

function ValidatePandocExecutable{
    [CmdletBinding()]
    param()

    if(Test-Path "$PSScriptRoot\Pandoc\pandoc.exe")
    {
        Write-Verbose "Pandoc.exe exists, we can proceed!"
    }
    else {
        Write-Verbose "Pandoc.exe doesn't exist we need to extract it"
        ExtractPanDoc
    }
}

function ConvertFrom{
    param(
        [Parameter(Mandatory=$true)]
        [string]$Path,

        [Parameter(Mandatory=$true)]
        [string]$TargetFormat,
        
        [string]$OutputDirectory,
        [string]$OutputFileName,
        [string]$SourceFormat,
        
        [switch]$ExtractImages
    )
    
    ValidatePandocExecutable

    Write-Verbose "Path to docx document: $Path"
    Write-Verbose "Chosen target format $TargetFormat"
    
    if($OutputDirectory -eq '')
    {
        $OutputDirectory ='.\';
        Write-Verbose "Output directory empty so changed to $OutputDirectory"
    }
    Write-Verbose "Output directory $OutputDirectory"
    New-Item -ItemType Directory -Force -Path $OutputDirectory
    
    $extension='.txt'
    if ($TargetFormat -eq 'html')
    {
        $extension='html'    
    }
    
    if ($TargetFormat -eq 'Markdown')
    {
        $extension='md'    
    }
    
    if($OutputFileName -eq '')
    {
        $OutputFileName ="Result.$extension";
        Write-Verbose "OutputFileName empty so changed to $OutputFilename"
    }
    
    Write-Verbose "Output OutputFileName: $OutputFileName"
    
    $OutputFullName=Join-Path $OutputDirectory $OutputFileName
    Write-Verbose "Output FullName: $OutputFullName"
    
    $OutputFilesPath=Join-Path $OutputDirectory "Files"

    $scriptPath= $PSScriptRoot
    $pandocPath="$scriptPath\Pandoc\pandoc.exe"

    Write-Verbose "Pandoc path: $pandocPath"
    if($ExtractImages)
    {
        $expression="'$pandocPath' '$Path' -f $SourceFormat -t $TargetFormat --output '$OutputFullName' --extract-media='$OutputFilesPath'"
    }
    else
    {
        [Array]$arguments = $Path, "-f" ,$SourceFormat, "-t", $TargetFormat, "--output", $OutputFullName;
    }
    Write-Verbose  "Executing command: $pandocPath"
    Write-Verbose  "With arguments: $arguments"

    & $pandocPath $arguments
    
}


function ConvertFrom-Markdown{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Path,

        [Parameter(Mandatory=$true)]
        [ValidateSet("Html", "Docx")]
        [string]$TargetFormat,
        
        [string]$OutputDirectory,
        [string]$OutputFileName,
        
        [switch]$ExtractImages
    )
    
    Write-Verbose "Hello from ConvertFrom-Markdown"
    ConvertFrom $Path $TargetFormat $OutputDirectory $OutputFileName "Markdown" -ExtractImages:$ExtractImages
}

function ConvertFrom-Docx {
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Path,

        [Parameter(Mandatory=$true)]
        [ValidateSet("Html", "Markdown")]
        [string]$TargetFormat,
        
        [string]$OutputDirectory,
        [string]$OutputFileName,
        
        [switch]$ExtractImages
    )
    

    Write-Verbose "Hello from ConvertFrom-Docx"
    ConvertFrom $Path $TargetFormat $OutputDirectory $OutputFileName "Docx"  -ExtractImages:$ExtractImages
}

Export-ModuleMember ConvertFrom-Docx
Export-ModuleMember ConvertFrom-Markdown