File: cml.hpp

package info (click to toggle)
higan 098-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,904 kB
  • ctags: 13,286
  • sloc: cpp: 108,285; ansic: 778; makefile: 32; sh: 18
file content (103 lines) | stat: -rw-r--r-- 3,028 bytes parent folder | download
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
#pragma once

/* CSS Markup Language (CML) v1.0 parser
 * revision 0.02
 */

namespace nall { namespace {

struct CML {
  auto& setPath(const string& pathname) { settings.path = pathname; return *this; }
  auto& setReader(const function<string (string)>& reader) { settings.reader = reader; return *this; }

  auto parse(const string& filename) -> string;
  auto parse(const string& filedata, const string& pathname) -> string;

private:
  struct Settings {
    string path;
    function<string (string)> reader;
  } settings;

  struct State {
    string output;
  } state;

  struct Variable {
    string name;
    string value;
  };
  vector<Variable> variables;

  auto parseDocument(const string& filedata, const string& pathname, uint depth) -> bool;
};

auto CML::parse(const string& filename) -> string {
  if(!settings.path) settings.path = pathname(filename);
  string document = settings.reader ? settings.reader(filename) : string::read(filename);
  parseDocument(document, settings.path, 0);
  return state.output;
}

auto CML::parse(const string& filedata, const string& pathname) -> string {
  settings.path = pathname;
  parseDocument(filedata, settings.path, 0);
  return state.output;
}

auto CML::parseDocument(const string& filedata, const string& pathname, uint depth) -> bool {
  if(depth >= 100) return false;  //prevent infinite recursion

  auto vendorAppend = [&](const string& name, const string& value) {
    state.output.append("  -moz-", name, ": ", value, ";\n");
    state.output.append("  -webkit-", name, ": ", value, ";\n");
  };

  for(auto& block : filedata.split("\n\n")) {
    lstring lines = block.rstrip().split("\n");
    string name = lines.takeFirst();

    if(name.beginsWith("include ")) {
      name.ltrim("include ", 1L);
      string filename{pathname, name};
      string document = settings.reader ? settings.reader(filename) : string::read(filename);
      parseDocument(document, nall::pathname(filename), depth + 1);
      continue;
    }

    if(name == "variables") {
      for(auto& line : lines) {
        auto data = line.split(":", 1L).strip();
        variables.append({data(0), data(1)});
      }
      continue;
    }

    state.output.append(name, " {\n");
    for(auto& line : lines) {
      auto data = line.split(":", 1L).strip();
      auto name = data(0), value = data(1);
      while(auto offset = value.find("var(")) {
        bool found = false;
        if(auto length = value.findFrom(*offset, ")")) {
          string name = slice(value, *offset + 4, *length - 4);
          for(auto& variable : variables) {
            if(variable.name == name) {
              value = {slice(value, 0, *offset), variable.value, slice(value, *offset + *length + 1)};
              found = true;
              break;
            }
          }
        }
        if(!found) break;
      }
      state.output.append("  ", name, ": ", value, ";\n");
      if(name == "box-sizing") vendorAppend(name, value);
    }
    state.output.append("}\n\n");
  }

  return true;
}

}}