// wikigen.cpp -- the wikitext/wikicode emanation (syntax 2.4, third output). // =========================================================================== #include "wikigen.h" #include "ast.h" #include "../../ledger/ledger_core.h" #include #include #include #include #include namespace sleela { static std::string baseName(const std::string& path){ size_t slash=path.find_last_of("/\\"); return slash==std::string::npos?path:path.substr(slash+1); } static std::string dirName(const std::string& path){ size_t slash=path.find_last_of("/\\"); return slash==std::string::npos?std::string():path.substr(0,slash); } static std::string stripExt(const std::string& name,const std::string& ext){ if(name.size()>=ext.size()&&name.compare(name.size()-ext.size(),ext.size(),ext)==0) return name.substr(0,name.size()-ext.size()); return name; } // The source edit date (YYYY-MM-DD) from the file mtime; today on failure. static std::string editDate(const std::string& path){ struct stat st; time_t t; if(stat(path.c_str(),&st)==0) t=st.st_mtime; else t=time(nullptr); struct tm tmv; #if defined(_WIN32) localtime_s(&tmv,&t); #else localtime_r(&t,&tmv); #endif char buf[16]; strftime(buf,sizeof(buf),"%Y-%m-%d",&tmv); return std::string(buf); } // The parameter type list restricted to the tokens that appear; only int and // String are brander-eligible, matching the originated-document contract. static std::string paramTypeList(const Method& m){ std::string out; bool first=true; for(const auto& p:m.params){ if(!first)out+=","; first=false; out+=p.type; } return out; } static std::string paramFull(const Method& m){ std::string out; bool first=true; for(const auto& p:m.params){ if(!first)out+=", "; first=false; out+=p.type+" "+p.name; } return out; } static bool eligible(const Method& m){ for(const auto& p:m.params) if(p.type!="int"&&p.type!="String") return false; return true; } static std::string brand(const std::string& doc,const std::string& member, const std::string& sig,const std::string& date){ return ledger::sha256_hex(doc+"|"+member+"|"+sig+"|"+date); } std::string writeWikitextDocument(const Program& prog, const std::string& srcPath, const std::string& wikiDir){ const std::string doc=baseName(srcPath); const std::string date=editDate(srcPath); const std::string stamp=ledger::utc_iso8601(); // the compile moment (the "second") std::string outDir=wikiDir.empty()?dirName(srcPath):wikiDir; std::string outBase=stripExt(doc,".sleela"); std::string outPath=(outDir.empty()?outBase:outDir+"/"+outBase)+".wiki"; // Tally the work's dimensions so the output can scale with the program. int classCount=(int)prog.classes.size(); int fieldCount=0, methodCount=0, dupNameCount=0; for(const auto& c:prog.classes){ fieldCount+=(int)c.fields.size(); methodCount+=(int)c.methods.size(); std::map nc; for(const auto& m:c.methods) nc[m.name]++; for(auto& kv:nc) if(kv.second>1) dupNameCount++; } std::ofstream o(outPath,std::ios::trunc); if(!o){ std::cerr<<"wikigen: cannot write '"<\n"; o<<"{{Infobox software\n"; o<<"| name = "<|||.\n\n"; for(const auto& c:prog.classes){ o<<"=== Class "<"< || " < || || "<\n"; } std::map nameCount; for(const auto& m:c.methods) nameCount[m.name]++; std::map nameSeen; for(const auto& m:c.methods){ ++id; std::string sig=paramTypeList(m); bool ok=eligible(m); std::string h=ok?brand(doc,m.name,sig,date):std::string("n/a"); int dupTotal=nameCount[m.name]; int dupSlot=++nameSeen[m.name]; std::string slot = dupTotal>1 ? (std::to_string(dupSlot)+"/"+std::to_string(dupTotal)) : std::string(""); o<<"|-\n| "<"< || " < || "<"<<(ok?h.substr(0,16):h)<<"\n"; } o<<"|}\n\n"; // Duplicate-dispatch ordering table, if any. bool anyDup=false; for(auto& kv:nameCount) if(kv.second>1){anyDup=true;break;} if(anyDup){ o<<"==== Duplicate-dispatch ordering ("<"< || ["; for(int s=1;s<=kv.second;++s){ if(s>1)o<<", "; o<<"slot "<"< \u2014 field\n"; for(const auto& m:c.methods) o<<"## "< \u2014 method\n"; o<<"\n"; } // ---- Provenance + categories ------------------------------------------ o<<"== Provenance ==\n"; o<<"This page was emanated by the SLeeLa compiler (Sleelvac\u2122) from the " <<"author-marked document "<. It is factual and " <<"structured; it asserts nothing beyond the document's own declared " <<"structure and timestamps.\n\n"; o<<"* Classes: "<