summaryrefslogtreecommitdiffstats
path: root/tools/Sandcastle/Source/BuildAssembler/BuildComponents/CodeReference.cs
blob: c0cbd1136c7eb88550176e9835a2dc71a6fffbf7 (plain)
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
// Copyright © Microsoft Corporation.
// This source file is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;


namespace Microsoft.Ddue.Tools {

    public enum CodeReferenceType {
        Invalid,    // not initialized, invalid reference string
        Snippet,    // MSDN style snippet for all build targets
        Msdn,       // MSDN style snippet for MSDN build only
        Run,        // runnable code sample with pop-up source browser for WebDocs build
        View        // code sample shown in pop-up source browser for WebDocs build
    }

    /// <summary>
    /// The CodeReference class encapsulates DDUE code reference elements and provides easy access
    /// to individual code reference parts, such as the type, name, title, etc..
    /// </summary>
    /// <remarks>
    /// Examples of valid code reference strings include:
    ///   - SampleName#SnippetNumber
    ///   - SampleName#SnippetNumber1,SnippetNumber2,SnippetNumber3
    ///   - msdn:SampleName#SnippetNumber
    ///   - run:SampleName
    ///   - run:SampleName;title text
    ///   - run:SampleName#startPage.aspx
    ///   - run:SampleName/path/to/startPage.aspx
    ///   - run:SampleName#startPage.aspx;title text
    ///   - run:SampleName/path/to/startPage.aspx;title text
    ///   - view:SampleName
    ///   - view:SampleName#defaultFile
    ///   - view:SampleName/path/to/defaultFile
    ///   - view:SampleName#defaultFile;title text
    ///   - view:SampleName/path/to/defaultFile;title text
    /// </remarks>
    public class CodeReference {

        string _ddueCodeReference;
        public string DdueCodeReference {
            get { return _ddueCodeReference; }
        }

        CodeReferenceType _type;
        public CodeReferenceType Type {
            get { return _type; }
        }

        string _exampleName;
        public string ExampleName {
            get { return _exampleName; }
        }

        string _examplePath;
        public string ExamplePath {
            get { return _examplePath; }
        }

        string _snippetId;
        public string SnippetId {
            get { return _snippetId; }
        }

        string _title;
        public string Title {
            get { return _title; }
        }

        public CodeReference(string ddueCodeReference) {
            _ddueCodeReference = ddueCodeReference;
            Parse();
        }

        static Regex codeReferenceRx = new Regex(
            @"^\s*(" +
                @"((?<type>msdn|run|view):)?" +
                @"(?<examplePath>(" +
                    @"(?<exampleName>[\w\.,\-]+)" +
                    @"((#(?<snippetId>[\w,]+))|(([/\\#,\.\w\-]+)?))" +
                @"))" +
                @"(;(?<title>.*))?" +
            @")\s*$",
            RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.Compiled);

        void Parse() {
            Match m = codeReferenceRx.Match(DdueCodeReference);
            if (m.Success) {
                _exampleName = m.Groups["exampleName"].Value;
                _snippetId = m.Groups["snippetId"].Value;
                _examplePath = m.Groups["examplePath"].Value;
                _title = m.Groups["title"].Value;
                // The default value of _type is CodeReferenceType.Invalid, if it isn't set in the following
                // block.
                if (m.Groups["type"].Length > 0) {
                    try {
                        _type = (CodeReferenceType)Enum.Parse(typeof(CodeReferenceType), m.Groups["type"].Value, true);
                    }
                    catch (ArgumentException) {
                        // _type = CodeReferenceType.Invalid
                    }
                }
                else if (m.Groups["exampleName"].Length > 0 && m.Groups["snippetId"].Length > 0) {
                    _type = CodeReferenceType.Snippet;
                }
            }
        }

        public override string ToString() {
            return DdueCodeReference;
        }
    }
}